0
0
Flaskframework~20 mins

Testing forms and POST data in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask POST Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when submitting a POST form in Flask?

Consider this Flask route handling a POST form submission:

from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    name = request.form.get('name', 'Guest')
    return f'Hello, {name}!'

If a form sends name=Alice in the POST data, what will the response be?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    name = request.form.get('name', 'Guest')
    return f'Hello, {name}!'
AHello, Guest!
BHello, Alice!
CError: Missing name
DHello, None!
Attempts:
2 left
💡 Hint

Check how request.form.get works with the given key.

📝 Syntax
intermediate
1:30remaining
Which Flask route decorator correctly accepts POST data?

Which of the following route decorators correctly allows a Flask route to accept POST requests?

A@app.route('/data', methods=['POST'])
B@app.route('/data', method='POST')
C@app.route('/data', methods='POST')
D@app.route('/data', post=True)
Attempts:
2 left
💡 Hint

Look at the correct parameter name and its expected type for methods.

🔧 Debug
advanced
2:30remaining
Why does this Flask POST handler raise a KeyError?

Given this Flask route:

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    return f'User: {username}'

When a POST request is sent without the 'username' field, what error occurs and why?

Flask
@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    return f'User: {username}'
AValueError because username is empty string
BTypeError because request.form is not a dict
CNo error, returns 'User: None'
DKeyError because 'username' is missing in form data
Attempts:
2 left
💡 Hint

Accessing a missing key in a dictionary-like object raises which error?

state_output
advanced
2:30remaining
What is the value of 'count' after multiple POST requests?

Consider this Flask app snippet:

from flask import Flask, request
app = Flask(__name__)
count = 0

@app.route('/increment', methods=['POST'])
def increment():
    global count
    count += 1
    return str(count)

If three POST requests are sent sequentially to '/increment', what will be the response to the third request?

Flask
from flask import Flask, request
app = Flask(__name__)
count = 0

@app.route('/increment', methods=['POST'])
def increment():
    global count
    count += 1
    return str(count)
AError: count is not defined
B0
C3
D1
Attempts:
2 left
💡 Hint

Think about how global variables behave across requests in a running Flask app.

🧠 Conceptual
expert
3:00remaining
Why is using global variables for POST data state problematic in Flask?

In a Flask app, why is it generally a bad idea to use global variables to store data from POST requests for tracking user state?

ABecause Flask runs multiple threads or processes, global variables may not be shared or synchronized, causing inconsistent state
BBecause global variables automatically reset after each POST request
CBecause POST data cannot be accessed inside functions using global variables
DBecause global variables cause syntax errors in Flask routes handling POST data
Attempts:
2 left
💡 Hint

Consider how Flask handles multiple users and requests in production.