0
0
Flaskframework~10 mins

HTML forms with POST method in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HTML forms with POST method
User fills form fields
User clicks Submit button
Browser sends POST request
Flask server receives POST data
Server processes data
Server sends response page
Browser displays response
This flow shows how a user fills a form, submits it via POST, the server processes the data, and sends back a response.
Execution Sample
Flask
from flask import Flask, request, render_template_string
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def form():
    if request.method == 'POST':
        name = request.form['name']
        return f'Hello, {name}!'
    return '''<form method='POST'><input name='name'><button type='submit'>Submit</button></form>'''
A simple Flask app that shows a form and greets the user by name after POST submission.
Execution Table
StepActionRequest MethodForm DataServer Response
1User visits pageGETNoneShow form with input and submit button
2User types 'Alice' in inputGETNoneForm still shown, no change
3User clicks SubmitPOSTname=AliceServer reads 'Alice' and returns 'Hello, Alice!'
4Browser displays greetingPOSTname=AlicePage shows: Hello, Alice!
5User refreshes pageGETNoneForm shown again, ready for new input
💡 Execution stops after server sends response and browser displays it.
Variable Tracker
VariableStartAfter Step 3After Step 5
request.methodGETPOSTGET
request.form['name']N/AAliceN/A
nameN/AAliceN/A
Key Moments - 3 Insights
Why does the server check if request.method is 'POST'?
Because the form page is shown on GET requests, but the server only processes submitted data on POST requests, as shown in execution_table step 3.
What happens if the user refreshes the page after submitting the form?
The browser sends a GET request again, so the form is shown empty, as in execution_table step 5.
Why is the form's method attribute set to 'POST'?
To tell the browser to send form data in the request body, not in the URL, which matches the server expecting POST requests (execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of request.method at step 3?
APOST
BGET
CPUT
DDELETE
💡 Hint
Check the 'Request Method' column at step 3 in the execution_table.
At which step does the server read the form data 'name=Alice'?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Form Data' and 'Action' columns in execution_table.
If the form's method was changed to 'GET', what would change in the execution table?
ARequest Method would be POST at step 3 but no form data sent
BRequest Method would be GET at step 3 and form data sent in URL
CServer would not receive any data
DBrowser would not send any request
💡 Hint
Think about how GET sends data compared to POST, and check the 'Request Method' column.
Concept Snapshot
HTML forms with POST method:
- Use <form method='POST'> to send data securely in request body.
- Server checks if request.method == 'POST' to process data.
- Access submitted data via request.form['fieldname'] in Flask.
- After processing, server returns response (e.g., greeting).
- GET requests show the form; POST requests handle submission.
Full Transcript
This visual trace shows how an HTML form with method POST works in a Flask app. The user visits the page (GET), sees a form, types a name, and submits it. The browser sends a POST request with the form data. The Flask server checks if the request method is POST, reads the form data from request.form, and returns a greeting message. The browser then displays this greeting. Refreshing the page sends a GET request again, showing the form anew. Key points include the difference between GET and POST, how form data is sent, and how the server processes it.