0
0
Flaskframework~10 mins

Login form and verification in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Login form and verification
User opens login page
Display login form
User enters username & password
User submits form
Server receives data
Verify credentials
Login success
Redirect to
dashboard
The user opens the login page, fills the form, submits it, then the server checks credentials and either logs in or shows an error.
Execution Sample
Flask
from flask import Flask, request, render_template_string
app = Flask(__name__)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        if username == 'user' and password == 'pass':
            return 'Login successful'
        else:
            return 'Login failed'
    return render_template_string('<form method="post">Username: <input name="username"><br>Password: <input name="password" type="password"><br><input type="submit"></form>')
This Flask app shows a login form and checks if username and password match fixed values.
Execution Table
StepActionRequest MethodForm DataVerification ResultResponse
1User opens /login pageGETNoneN/AShow login form
2User fills form and submitsPOSTusername='user', password='pass'Check if username=='user' and password=='pass'Login successful
3User fills form and submitsPOSTusername='user', password='wrong'Check failsLogin failed
4User opens /login page againGETNoneN/AShow login form
💡 Execution stops after sending response to user for each request.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
usernameNone'user''user'None
passwordNone'pass''wrong'None
request.methodGETPOSTPOSTGET
verification_resultN/ATrueFalseN/A
Key Moments - 2 Insights
Why does the form show again after a failed login?
Because when verification fails (see step 3 in execution_table), the server returns 'Login failed' but the user can try again by reloading the form (step 4). The form is shown on GET requests.
Why do we check request.method before accessing form data?
Because form data is only sent on POST requests. On GET requests, there is no form data, so we show the form instead (see steps 1 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response when username='user' and password='pass'?
ALogin successful
BLogin failed
CShow login form
DError page
💡 Hint
Check step 2 in execution_table where form data matches correct credentials.
At which step does the server receive a GET request?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the Request Method column in execution_table for GET requests.
If the password check was changed to 'password == "1234"', what would be the response at step 2?
ALogin successful
BLogin failed
CShow login form
DServer error
💡 Hint
Compare the password in form data at step 2 with the new check condition.
Concept Snapshot
Flask login form:
- Use @app.route with GET and POST
- On GET, show form HTML
- On POST, get username/password from request.form
- Check credentials with if condition
- Return success or failure message
- Form resubmits on failure for retry
Full Transcript
This example shows a simple login form in Flask. When the user visits the /login page with a GET request, the server sends back a form with username and password fields. When the user fills the form and submits it, the browser sends a POST request with the form data. The server then checks if the username and password match the expected values. If they do, it returns a success message. If not, it returns a failure message. The user can then try again by reloading the form. This flow repeats for each request.