Ex51, am I understanding transition between hello_form.html and index.html correctly?

I am talking specifically about app.py

from flask import Flask
from flask import render_template
from flask import request

app = Flask(__name__)

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

def index():
    greeting = "Hello World"

    if request.method == "POST":
        name = request.form["name"]
        greet = request.form["greet"]
        greeting = f"{greet}, {name}"

        return render_template("index.html", greeting=greeting)

    else:
        return render_template("hello_form.html")

if __name__ == "__main__":

    app.run()

So, am I understanding this code correctly:

When flask is reaching index, request method is “GET”, so hello_form.html is rendered. But upon pressing “Submit” on our website, whole app.py gets re-initialized but this time, request method is “POST”, so we get index.html, right?

Thanks in advance!

to my understanding this is correct

1 Like

Thank you for confirming, kind sir.

1 Like

You’re basically right, except for the fact that app.py is not re-initialized. It’s just still listening for requests, and whenever a request is sent to the route /hello, the function index is called to generate a new response.

A free service run by Zed A. Shaw for learncodethehardway.org.