Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

python - Flask - 405 Method not allowed with methods=['GET, POST']

bp_booking = Blueprint('booking', __name__, url_prefix='/booking', template_folder='templates/booking')

@bp_booking.route('<screening>',  methods=["GET", "POST"])
def seat_select(screening):
    cur = db.connection.cursor()
    cur.execute("""SELECT M.title, S.Screening_Start
                    FROM screening S JOIN movie M ON s.movie_id = M.id
                    WHERE S.id = (%s)""", (screening,))
    screening_details = cur.fetchone()
    title = screening_details[0]
    time = screening_details[1].strftime('%d %B  %H:%M')
    cur.execute("""SELECT A.row_count, A.column_count
                    FROM auditorium A JOIN screening S on S.auditorium_id = A.id
                    WHERE S.id = (%s)""", (screening,))
    screen_no = cur.fetchone()
    row = screen_no[0]
    column = screen_no[1]

   
    return render_template('booking/seatSelect.html', screeningId=screening,
                           title=title, time=time, rows=row, columns=column)


@bp_booking.route('/process-ticket', methods=['GET, POST'])
def process_ticket():
    ticket_value = request.form.get('hidden-ticket-value')
    ## insert into database here
    return 'Ticket inserted into database'

seatSelect.html

<div id="container">
</div>

<div id = "right">
<p id= "quantity">
 Select your seats!
 </p>

    <form method="POST" action="{{ url_for('booking.process_ticket') }}">
    <input type="hidden" id="hidden" name="hidden-ticket-value">

    <input type="submit" value="Book" id="book-btn">
    </form>

</div>

</body>

seatSelect.html has some additonal javascript and css to make a seating plan and the selected seats are stored in the hidden field's value.

When clicking submit I want to get routed to process-ticket
<form method="POST" action="{{ url_for('booking.process_ticket') }}">
but instead the seat_select(screening) function called called again with the parameter screening=process-ticket This just throws and error because my function expects a number. Any idea why this is happening?

I tried hardcoding the HTML to take the URL for process_ticket() and the same thing happens.

Edit: I changed the route of select_seat() slightly and now process_ticket() seems to be running but i'm getting a 405 method not allowed error. Should this be happening if methods=['GET, POST'] is present in the function?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As for me poblem can be because <screening> can match any string - even /process-ticket - and route("<screening>"...) is check as first - before route("/process-ticket")

Flask first compares address /process-ticket with route("<screening>"...) and it matches so it doesn't check with route("/process-ticket") but runs seat_select(screening="process-ticket").

You have to put function def process_ticket() before def seat_select() and then flask will check address /process-ticket with route("/process-ticket") as first.

Or you should use <int:screening> to match only number and then address /process-ticket will not match to <int:screening>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...