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'?
Given this Flask route, what will be printed when a form with a field 'username' set to 'alice' is submitted via POST?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/submit', methods=['POST']) def submit(): user = request.form.get('username') return f'Hello, {user}!'
Attempts:
2 left
💡 Hint
Remember that form data is accessed via request.form in Flask routes handling POST requests.
✗ Incorrect
The route uses request.form.get('username') which safely returns the value 'alice' from the submitted form data. So the output is 'Hello, alice!'.
🔧 Debug
intermediate2:00remaining
Which option causes a runtime error when accessing form data?
Identify the option that will cause a runtime error in a Flask route when trying to get form data.
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/login', methods=['POST']) def login(): # Access form data here pass
Attempts:
2 left
💡 Hint
Check for calling methods vs indexing on methods.
✗ Incorrect
Option C uses square brackets on get, which is a method, causing TypeError: 'method' object is not subscriptable. The correct way is get('key').
❓ state_output
advanced2:00remaining
What is the value of 'email' after submitting a form without the 'email' field?
Consider this Flask route:
@app.route('/register', methods=['POST'])
def register():
email = request.form.get('email', 'not_provided')
return email
What will be returned if the submitted form does NOT include an 'email' field?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/register', methods=['POST']) def register(): email = request.form.get('email', 'not_provided') return email
Attempts:
2 left
💡 Hint
What does get return if the key is missing and a default is given?
✗ Incorrect
The get method returns the default value 'not_provided' when the key 'email' is missing in form data.
🔧 Debug
advanced2:00remaining
Which option causes a runtime error when accessing form data?
Given this Flask route, which option will cause a runtime error when the form is submitted?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/update', methods=['POST']) def update(): # Access form data here pass
Attempts:
2 left
💡 Hint
Consider what happens if 'age' is missing and get returns None.
✗ Incorrect
Option B calls int(None) if 'age' is missing, causing a TypeError at runtime.
🧠 Conceptual
expert3:00remaining
Which option correctly accesses multiple values for the same form field?
In Flask, when a form submits multiple values for the same field name 'colors', which option correctly retrieves all values as a list?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/colors', methods=['POST']) def colors(): # Retrieve all 'colors' values pass
Attempts:
2 left
💡 Hint
Check Flask's method for getting multiple values from form data.
✗ Incorrect
The getlist method returns all values for a given key as a list, which is needed when multiple values are submitted.