📖 Scenario: You are building a simple web page where users can submit their name and email address. The data will be sent securely to the server using the POST method.
🎯 Goal: Create a Flask app with an HTML form that uses the POST method to send user input to the server. The server should receive the data and store it in a dictionary.
📋 What You'll Learn
Create a Flask app with a route for the form page
Create an HTML form with method='POST' and input fields for name and email
Add a route to handle the POST request and store the submitted data in a dictionary
Display a confirmation message after submission
💡 Why This Matters
🌍 Real World
Websites often need forms to collect user information securely. Using POST method ensures data is sent safely to the server.
💼 Career
Understanding how to create and handle HTML forms with POST in Flask is essential for backend web development roles.
Progress0 / 4 steps
1
Set up Flask app and initial data storage
Create a Flask app by importing Flask and request from flask. Then create an empty dictionary called submissions to store user data.
Flask
Hint
Remember to import both Flask and request from the flask package.
2
Create HTML form with POST method
Add a route @app.route('/') with a function form() that returns an HTML form string. The form must have method='POST' and input fields with name='name' and name='email'. Include a submit button.
Flask
Hint
Use triple quotes ''' to return multi-line HTML as a string.
3
Handle POST request and store data
Modify the form() function to accept both GET and POST methods by adding methods=['GET', 'POST'] to the route. Inside the function, check if the request method is POST. If yes, get the name and email from request.form and store them in the submissions dictionary with name as the key and email as the value.
Flask
Hint
Use request.method == 'POST' to check the form submission.
4
Run the Flask app
Add the code to run the Flask app by checking if __name__ == '__main__' and calling app.run(debug=True).
Flask
Hint
This code runs the Flask app when you start the script.
Practice
(1/5)
1. What does the POST method in an HTML form do in a Flask application?
easy
A. Automatically encrypts the form data before sending
B. Sends form data appended to the URL as query parameters
C. Prevents the form from submitting data
D. Sends form data inside the request body, not visible in the URL
Solution
Step 1: Understand HTTP methods in forms
The POST method sends data inside the request body, keeping it hidden from the URL.
Step 2: Compare with GET method
The GET method appends data to the URL, but POST does not, making it safer for sensitive data.
Final Answer:
Sends form data inside the request body, not visible in the URL -> Option D
Quick Check:
POST hides data in request body [OK]
Hint: POST sends data hidden in body, not URL [OK]
Common Mistakes:
Confusing POST with GET method
Thinking POST encrypts data automatically
Believing POST prevents form submission
2. Which of the following is the correct way to specify a form that sends data using POST in HTML?
easy
A. ...
B. ...
C. ...
D. ...
Solution
Step 1: Identify the correct attribute for HTTP method
The method attribute in the form tag defines how data is sent.
Step 2: Confirm correct value for POST
Setting method='post' correctly configures the form to send data via POST.
Final Answer:
<form method='post'>...</form> -> Option A
Quick Check:
Use method='post' in form tag [OK]
Hint: Use method='post' attribute in form tag [OK]
Common Mistakes:
Using 'action' instead of 'method' for HTTP method
Writing method='get' when POST is needed
Using non-standard attributes like 'send'
3. Given this Flask route and form, what will be printed when the form is submitted?
from flask import Flask, request
app = Flask(__name__)
@app.route('/submit', methods=['POST'])
def submit():
name = request.form.get('name')
return f'Hello, {name}!'
Send
medium
A. Hello, !
B. Hello, Alice!
C. Error: name not found
D. Hello, request.form!
Solution
Step 1: Understand form submission with POST
The form sends the input named 'name' with value 'Alice' to the '/submit' route via POST.
Step 2: Check how Flask reads form data
The route uses request.form.get('name') to get the value 'Alice' from the form data.
Final Answer:
Hello, Alice! -> Option B
Quick Check:
request.form.get('name') returns 'Alice' [OK]
Hint: request.form.get('name') reads POST form data [OK]
Common Mistakes:
Using request.args instead of request.form for POST data
Not specifying methods=['POST'] in route
Missing the 'name' attribute in input
4. What is wrong with this Flask route that handles a POST form submission?
A. The route does not allow POST method, so form data won't be received
B. request.form['username'] should be request.args['username']
C. The route should use GET method instead of POST
D. Flask requires form data to be accessed via request.data
Solution
Step 1: Check route methods
The route only allows GET by default, but form data sent by POST won't be processed.
Step 2: Confirm correct method usage
To handle POST form data, the route must include methods=['POST'].
Final Answer:
The route does not allow POST method, so form data won't be received -> Option A
Quick Check:
Route must allow POST to read POST form data [OK]
Hint: Add methods=['POST'] to route decorator [OK]
Common Mistakes:
Forgetting to add methods=['POST'] in route
Using request.args for POST data
Trying to access form data via request.data
5. You want to create a Flask route that accepts a POST form with fields 'username' and 'password'. It should return 'Login successful' only if username is 'admin' and password is 'secret', otherwise 'Login failed'. Which code snippet correctly implements this?
hard
A. from flask import Flask, request
app = Flask(__name__)
@app.route('/login', methods=['GET'])
def login():
user = request.form['username']
pwd = request.form['password']
if user == 'admin' and pwd == 'secret':
return 'Login successful'
else:
return 'Login failed'
B. from flask import Flask, request
app = Flask(__name__)
@app.route('/login')
def login():
user = request.args.get('username')
pwd = request.args.get('password')
if user == 'admin' and pwd == 'secret':
return 'Login successful'
else:
return 'Login failed'
C. from flask import Flask, request
app = Flask(__name__)
@app.route('/login', methods=['POST'])
def login():
user = request.form.get('username')
pwd = request.form.get('password')
if user == 'admin' and pwd == 'secret':
return 'Login successful'
else:
return 'Login failed'
D. from flask import Flask, request
app = Flask(__name__)
@app.route('/login', methods=['POST'])
def login():
user = request.form['username']
if user == 'admin':
return 'Login successful'
else:
return 'Login failed'
Solution
Step 1: Ensure route accepts POST method
The route must specify methods=['POST'] to handle form POST data.
Step 2: Correctly read both 'username' and 'password' from request.form
Use request.form.get() to safely get both fields and check their values.
Step 3: Implement correct login logic
Return 'Login successful' only if both username and password match expected values; otherwise return 'Login failed'.
Final Answer:
The code with methods=['POST'], request.form.get() for both fields, and checks both credentials -> Option C
Quick Check:
from flask import Flask, request
app = Flask(__name__)
@app.route('/login', methods=['POST'])
def login():
user = request.form.get('username')
pwd = request.form.get('password')
if user == 'admin' and pwd == 'secret':
return 'Login successful'
else:
return 'Login failed' [OK]
Hint: Use methods=['POST'] and request.form.get for form fields [OK]
Common Mistakes:
Using GET method instead of POST
Reading form data from request.args
Checking only username without password
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.