0
0
Flaskframework~20 mins

Accessing form data in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Data 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 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
AError: KeyError
Balice
Cusername
DNone
Attempts:
2 left
💡 Hint
request.form.get('fieldname') fetches the value sent by the form for that field.
📝 Syntax
intermediate
2: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
Aemail = request.form['email']
Bemail = request.args.get('email')
Cemail = request.form.get('email')
Demail = request.data['email']
Attempts:
2 left
💡 Hint
Form data is accessed via request.form, and get() avoids errors if the key is missing.
🔧 Debug
advanced
2: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}'
AFlask does not support form data retrieval
BThe input field is named 'Age' with uppercase A
Crequest.form.get requires a default value argument
DThe form is submitted with method='GET' instead of 'POST'
Attempts:
2 left
💡 Hint
Check the form's method attribute and how Flask handles form data for different methods.
state_output
advanced
2: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}'
Ablue
Bred
CNone
DError: KeyError
Attempts:
2 left
💡 Hint
The second argument to get() is the default if the key is missing.
🧠 Conceptual
expert
2:00remaining
Which statement about accessing form data in Flask is TRUE?
Select the correct statement about how Flask handles form data in POST requests.
Arequest.form contains parsed form data only for POST requests with 'application/x-www-form-urlencoded' or 'multipart/form-data' content types
Brequest.form always contains JSON data sent in the request body
Crequest.form.get() raises a KeyError if the key is missing
Drequest.form is a list of tuples representing form fields
Attempts:
2 left
💡 Hint
Think about what content types Flask parses into request.form.