HTML forms with the POST method let users send data to a server safely. This is useful when you want to submit information like login details or comments.
When a user needs to send sensitive data like passwords.
When submitting a form that changes data on the server, like adding a comment.
When uploading files through a web page.
When you want to avoid showing form data in the URL.
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.