0
0
Flaskframework~20 mins

HTML forms with POST method in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTML Forms POST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Flask route receives a POST request?
Consider this Flask route handling a form submission with POST method. What will be the output if the form sends 'username' as 'alice'?
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    username = request.form.get('username')
    return f"Hello, {username}!"
AHello, None!
BHello, alice!
CMethod Not Allowed error
DInternal Server Error
Attempts:
2 left
💡 Hint
Remember that request.form contains data sent by POST method from the form.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Flask route to handle POST form data?
Select the option that correctly defines a Flask route to accept POST requests and retrieve a form field named 'email'.
A
@app.route('/email')
def email():
    email = request.form['email']
    return email
B
@app.route('/email', methods=['POST'])
def email():
    email = request.args['email']
    return email
C
@app.route('/email', methods=['GET'])
def email():
    email = request.form['email']
    return email
D
@app.route('/email', methods=['POST'])
def email():
    email = request.form['email']
    return email
Attempts:
2 left
💡 Hint
POST data is accessed via request.form, and the route must allow POST method.
🔧 Debug
advanced
2:00remaining
Why does this Flask form submission always return 'None' for the username?
Given this Flask route and HTML form, why does the server always respond with 'Hello, None!' even when a username is entered?
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/login', methods=['POST'])
def login():
    username = request.args.get('username')
    return f"Hello, {username}!"

# HTML form:
# <form action="/login" method="post">
#   <input type="text" name="username">
#   <input type="submit">
# </form>
ABecause request.args is used instead of request.form to get POST data.
BBecause the form method should be GET, not POST.
CBecause the input field is missing a 'value' attribute.
DBecause the route does not return a valid string.
Attempts:
2 left
💡 Hint
POST form data is in request.form, not request.args.
state_output
advanced
2:00remaining
What is the value of 'count' after submitting the form twice?
This Flask app counts how many times the form is submitted. What will be the value of 'count' after two POST submissions?
Flask
from flask import Flask, request
app = Flask(__name__)

count = 0

@app.route('/submit', methods=['POST'])
def submit():
    global count
    count += 1
    return f"Submission count: {count}"
ASubmission count: 0
BSubmission count: 1
CSubmission count: 2
DUnboundLocalError
Attempts:
2 left
💡 Hint
The global variable 'count' is incremented on each POST request.
🧠 Conceptual
expert
2:00remaining
Which statement about HTML forms with POST method and Flask is TRUE?
Select the true statement about handling HTML forms with POST method in Flask.
ATo handle POST data, the Flask route must specify methods=['POST'] or methods=['GET', 'POST'].
BFlask automatically parses JSON data sent by POST forms into request.form.
CForm data sent by POST is accessible via request.args in Flask.
DFlask's request.form only contains data sent by POST method, never by GET.
Attempts:
2 left
💡 Hint
Check how Flask routes handle HTTP methods and where form data is stored.