What is the first step Flask takes when it receives an HTTP request?
Flask receives the HTTP request and creates a Request object that holds all the details like method, URL, headers, and data.
Click to reveal answer
beginner
How does Flask decide which function should handle an incoming HTTP request?
Flask matches the request URL and method to a route defined by the developer using decorators like @app.route(). This tells Flask which function to run.
Click to reveal answer
beginner
What role does the 'view function' play in Flask's request processing?
The view function is the code that runs after Flask matches the route. It processes the request and returns a response, like HTML or JSON.
Click to reveal answer
beginner
What happens after the view function returns a response in Flask?
Flask converts the returned value into an HTTP response object and sends it back to the client (browser or API caller).
Click to reveal answer
intermediate
How does Flask handle errors or requests that don’t match any route?
Flask returns an error response like 404 Not Found if no route matches. Developers can customize error handling with special error handler functions.
Click to reveal answer
What object does Flask create to hold details of an incoming HTTP request?
ARoute object
BResponse object
CRequest object
DSession object
✗ Incorrect
Flask creates a Request object to store all information about the incoming HTTP request.
Which Flask decorator is used to link a URL to a function?
A@app.handle()
B@app.view()
C@app.request()
D@app.route()
✗ Incorrect
The @app.route() decorator tells Flask which URL should trigger the decorated function.
What does a Flask view function return?
AAn HTTP response or data to build one
BA URL string
CA Request object
DA database connection
✗ Incorrect
The view function returns data or a response that Flask converts into an HTTP response sent to the client.
What status code does Flask return if no route matches the request URL?
A404 Not Found
B200 OK
C500 Internal Server Error
D301 Moved Permanently
✗ Incorrect
Flask returns a 404 Not Found error when no route matches the requested URL.
Which step comes directly after Flask matches a route to a request?
ASend response to client
BRun the view function
CCreate a Request object
DLog the request
✗ Incorrect
After matching the route, Flask runs the view function to process the request.
Explain the journey of an HTTP request through Flask from arrival to response.
Think about what Flask does step-by-step when a browser asks for a page.
You got /4 concepts.
Describe how Flask handles a request that does not match any defined route.
What happens when you visit a page that does not exist in a Flask app?
You got /3 concepts.
Practice
(1/5)
1. What does Flask use to decide which function should handle an incoming HTTP request?
easy
A. The route URL pattern defined with @app.route
B. The order in which functions are defined in the code
C. The size of the HTTP request body
D. The IP address of the client
Solution
Step 1: Understand Flask routing
Flask uses route decorators like @app.route to map URLs to functions.
Step 2: Match incoming request URL
When a request comes, Flask matches its URL to the route patterns to find the right function.
Final Answer:
The route URL pattern defined with @app.route -> Option A
Quick Check:
Routing = @app.route [OK]
Hint: Routes connect URLs to functions in Flask [OK]
Common Mistakes:
Thinking function order matters
Confusing client IP with routing
Using request size to route
2. Which of the following is the correct way to define a Flask route that only accepts POST requests?
easy
A. @app.route('/submit', methods=['GET'])
B. @app.route('/submit', accept='POST')
C. @app.route('/submit', methods=['POST'])
D. @app.route('/submit', method='POST')
Solution
Step 1: Check correct syntax for methods argument
Flask route decorator uses methods=['POST'] to specify allowed HTTP methods.
Step 2: Identify the correct option
@app.route('/submit', methods=['POST']) uses methods=['POST'], which is the correct syntax.
Final Answer:
@app.route('/submit', methods=['POST']) -> Option C
Quick Check:
methods=['POST'] sets POST only [OK]
Hint: Use methods=['POST'] to allow only POST requests [OK]
Common Mistakes:
Using method instead of methods
Using accept instead of methods
Allowing GET instead of POST
3. Given this Flask code snippet:
from flask import Flask, request
app = Flask(__name__)
@app.route('/greet', methods=['POST'])
def greet():
name = request.form.get('name', 'Guest')
return f'Hello, {name}!'
What will be the response if a POST request is sent to '/greet' with form data name='Alice'?
medium
A. Hello, Guest!
B. Hello, Alice!
C. Hello, None!
D. Error: Missing name parameter
Solution
Step 1: Understand request.form.get usage
The code tries to get 'name' from form data; if missing, defaults to 'Guest'.
Step 2: Analyze the POST request data
The POST request includes name='Alice', so request.form.get('name') returns 'Alice'.
Final Answer:
Hello, Alice! -> Option B
Quick Check:
Form data name='Alice' returns 'Hello, Alice!' [OK]
Hint: request.form.get returns form value or default [OK]
C. Route decorator should not have methods argument
D. Function name cannot be 'data'
Solution
Step 1: Check syntax of if statement
The if statement lacks a colon at the end, causing a syntax error.
Step 2: Verify other parts
request.method is valid, methods argument is correct, function name is allowed.
Final Answer:
Missing colon after if statement -> Option A
Quick Check:
if statement needs colon [OK]
Hint: Check colons after if/else statements [OK]
Common Mistakes:
Forgetting colon after if
Thinking request.method is invalid
Misunderstanding methods argument
5. You want to create a Flask route '/submit' that accepts both GET and POST requests. When accessed by GET, it should show a form. When accessed by POST, it should process form data and return a confirmation message. Which code snippet correctly implements this behavior?
hard
A. from flask import Flask, request
app = Flask(__name__)
@app.route('/submit', methods=['GET', 'POST'])
def submit():
return 'Submit page'
B. from flask import Flask, request
app = Flask(__name__)
@app.route('/submit')
def submit():
if request.method == 'POST':
return 'Data received'
else:
return ''
C. from flask import Flask
app = Flask(__name__)
@app.route('/submit', methods=['POST'])
def submit():
return 'Only POST allowed'
D. from flask import Flask, request
app = Flask(__name__)
@app.route('/submit', methods=['GET', 'POST'])
def submit():
if request.method == 'POST':
data = request.form.get('data')
return f'Data received: {data}'
else:
return ''''''
Solution
Step 1: Check methods argument
from flask import Flask, request
app = Flask(__name__)
@app.route('/submit', methods=['GET', 'POST'])
def submit():
if request.method == 'POST':
data = request.form.get('data')
return f'Data received: {data}'
else:
return '''''' correctly uses methods=['GET', 'POST'] to accept both request types.
Step 2: Verify request method handling
from flask import Flask, request
app = Flask(__name__)
@app.route('/submit', methods=['GET', 'POST'])
def submit():
if request.method == 'POST':
data = request.form.get('data')
return f'Data received: {data}'
else:
return '''''' checks request.method and returns form HTML for GET and processes form data for POST.
Step 3: Compare other options
The other options either miss methods argument, do not handle both methods properly, or do not return form on GET.
Final Answer:
The code snippet that uses methods=['GET', 'POST'], checks request.method, returns form for GET and processes data for POST -> Option D
Quick Check:
methods=['GET','POST'] + if request.method [OK]
Hint: Use methods=['GET','POST'] and check request.method [OK]
Common Mistakes:
Not specifying methods argument
Not checking request.method
Returning same response for all methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.