I"m reading the flask web development book, and I"m confused about the processing logic of submitting forms to the server through POST.
the following is a code snippet from the book, which is the view function: corresponding to the root path
@app.route("/", methods=["GET", "POST"])
def index():
name = None
form = NameForm()
if form.validate_on_submit():
name = form.name.data
form.name.data = ""
return render_template("index.html", form=form, name=name)
Screenshot of the page:
my idea is: when the user visits the home page for the first time, the browser issues a GET request, and the server calls the index () function to process the request. form
is created by NameForm ()
, so form.validate_on_submit ()
returns False. However, when a user submits a form through POST, the server should still call the index () function to process the request, so doesn"t that create a new form through form = NameForm ()
? Should this new form be empty? Why does form.validate_on_submit ()
return True? at this time
how does flask handle POST requests involving forms such as the sample code?
Thank you.