Challenge - 5 Problems
Form Data Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when submitting a form with 'username=alice'?
Consider this Flask route handling a POST request from a form with an input named 'username'. What will be printed to the console?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/submit', methods=['POST']) def submit(): user = request.form.get('username') print(user) return 'Received' # Assume form sends username=alice
Attempts:
2 left
💡 Hint
request.form.get('fieldname') fetches the value sent by the form for that field.
✗ Incorrect
request.form.get('username') retrieves the value of the 'username' field from the submitted form data. Since the form sends 'username=alice', it prints 'alice'.
📝 Syntax
intermediate2:00remaining
Which option correctly accesses the 'email' field from form data?
Given a Flask route handling a POST request, which code correctly retrieves the 'email' field from the submitted form?
Flask
from flask import request # Inside a route function
Attempts:
2 left
💡 Hint
Form data is accessed via request.form, and get() avoids errors if the key is missing.
✗ Incorrect
request.form.get('email') safely retrieves the 'email' field from form data. request.form['email'] works but raises KeyError if missing. request.args.get accesses URL query parameters, not form data. request.data is raw bytes, not a dict.
🔧 Debug
advanced2:00remaining
Why does request.form.get('age') return None even though the form has an 'age' input?
A Flask route tries to get 'age' from form data but always gets None, even though the form includes an input named 'age'. What is the most likely cause?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/age', methods=['POST']) def age(): age_value = request.form.get('age') return f'Age is {age_value}'
Attempts:
2 left
💡 Hint
Check the form's method attribute and how Flask handles form data for different methods.
✗ Incorrect
If the form uses method='GET', form data is sent as URL parameters, accessible via request.args, not request.form. So request.form.get('age') returns None.
❓ state_output
advanced2:00remaining
What is the value of 'color' after this POST request?
Given this Flask route, what will be the value of the variable 'color' after a form submits 'color=blue'?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/color', methods=['POST']) def color_route(): color = request.form.get('color', 'red') return f'Color is {color}'
Attempts:
2 left
💡 Hint
The second argument to get() is the default if the key is missing.
✗ Incorrect
Since the form submits 'color=blue', request.form.get('color', 'red') returns 'blue'. The default 'red' is ignored because the key exists.
🧠 Conceptual
expert2:00remaining
Which statement about accessing form data in Flask is TRUE?
Select the correct statement about how Flask handles form data in POST requests.
Attempts:
2 left
💡 Hint
Think about what content types Flask parses into request.form.
✗ Incorrect
Flask parses form data into request.form only for POST requests with content types 'application/x-www-form-urlencoded' or 'multipart/form-data'. JSON data is accessed via request.get_json(). request.form.get() returns None if key missing, no error. request.form is a MultiDict, not a list.