0
@app.route("/form", methods=["POST"])

the

 methods=["POST"]

what specifically is that doing ?

is it just saying any time that

/form

is posting something the below functions will be triggered/activated and if that is true

@app.route("/form", methods=["GET"])
def get_form():
    return render_template("form.html")

what is

@app.route("/form", methods=["GET"])

getting ? That is constantly triggering the render_template("form.html")

0

The @app.route is a decorator, replaces get_form with another function, app.route("/form", methods=["GET"])(get_form) (app.route() returns a function that will decorate/wrap another function).

The first argument, "/form" names the route you want to serve this resource on.

The second argument, named argument methods=["GET"] (["GET"] is actually its default value), tells the wrapper which HTTP methods it should accept for this route (you will rarely see something different than GET and POST). route accepts a list of strings, so you could register a single function for multiple methods on the same route (and inside the function check request.method), or, IIRC only with a recent Flask version, you could have separate functions for the different methods, even if for the same route, and that's probably what you see in the provided code. Each of the two functions then reacts only to one specific route/method combination.

Handling both GET and POST for a single route is not that uncommon. It's usually a good idea to do something reasonable with GET requests for a route that a user might submit a form to (in case they copy the URL of the resulting page), even if you just redirect that user (if that's the reasonable choice).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .