What is the purpose of the @app.before_request decorator in Flask?
It registers a function to run before each request. This function can modify the request or perform checks like authentication before the main route handler runs.
Click to reveal answer
intermediate
How does @app.before_request act as a middleware alternative in Flask?
It allows you to run code before every request without creating a separate middleware class. This is simpler and fits Flask's design for request preprocessing.
Click to reveal answer
intermediate
Can you stop a request from continuing in a @app.before_request function? How?
Yes. By returning a response (like a redirect or error message) from the before_request function, Flask stops processing the request further and sends that response immediately.
Click to reveal answer
beginner
What is a common use case for @app.before_request in Flask apps?
Common uses include checking if a user is logged in, setting up database connections, or logging request details before the main route logic runs.
Click to reveal answer
advanced
How does @app.before_request differ from traditional middleware in other frameworks?
Traditional middleware often wraps the request and response cycle as separate components. Flask's before_request is a simpler hook that runs code before the route handler without wrapping the whole cycle.
Click to reveal answer
What happens if a @app.before_request function returns a response?
AThe request is retried.
BThe response is ignored and the route handler runs anyway.
CAn error is raised.
DThe response is sent immediately, skipping the route handler.
✗ Incorrect
Returning a response from a before_request function stops further processing and sends that response immediately.
Which of these is NOT a typical use of @app.before_request?
AModifying the response body after route handler
BLogging request details
CChecking user authentication
DSetting up database connections
✗ Incorrect
before_request runs before the route handler, so it cannot modify the response body after the handler.
In Flask, @app.before_request functions run:
ABefore the route handler
BOnly on GET requests
COnly on POST requests
DAfter the route handler
✗ Incorrect
before_request functions run before the route handler for every request.
Which Flask decorator would you use to run code after each request?
A@app.before_request
B@app.after_request
C@app.route
D@app.teardown_request
✗ Incorrect
@app.after_request runs after the route handler and can modify the response.
If you want to run code only for certain routes before the request, what should you do?
AUse a separate middleware class
BUse @app.route with a special flag
CUse @app.before_request and check the request path inside
DUse @app.after_request
✗ Incorrect
You can use @app.before_request and inside the function check request.path to run code conditionally.
Explain how @app.before_request works as a middleware alternative in Flask and give an example use case.
Think about what happens before your route code runs.
You got /4 concepts.
Describe how you can stop a request from reaching the route handler using @app.before_request.
Returning something other than None is key.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the before_request function in Flask?
easy
A. To send the final response to the client
B. To handle errors after a request has been processed
C. To define routes and their handlers
D. To run code before every request to perform tasks like login checks or logging
Solution
Step 1: Understand the role of before_request
The before_request function runs before each request to the Flask app, allowing pre-processing like login checks or logging.
Step 2: Compare with other Flask functions
Functions like error handlers run after requests, and route handlers define endpoints, so they differ from before_request.
Final Answer:
To run code before every request to perform tasks like login checks or logging -> Option D
Quick Check:
before_request runs before requests = D [OK]
Hint: before_request runs code before requests start [OK]
Common Mistakes:
Confusing before_request with error handlers
Thinking before_request sends responses directly
Mixing route definition with before_request
2. Which of the following is the correct way to register a before_request function in Flask?
easy
A. @app.before_request\ndef my_function(): pass
B. app.before_request(my_function())
C. app.before_request = my_function()
D. app.before_request = my_function
Solution
Step 1: Recall Flask decorator syntax
Flask uses decorators like @app.before_request above a function to register it as a before_request handler.
Step 2: Analyze each option
@app.before_request\ndef my_function(): pass uses the correct decorator syntax. app.before_request(my_function()) calls the function instead of decorating. Options C and D assign incorrectly.
Final Answer:
@app.before_request\ndef my_function(): pass -> Option A
Quick Check:
Use @app.before_request decorator = A [OK]
Hint: Use @app.before_request decorator above the function [OK]
Common Mistakes:
Calling the function instead of decorating
Assigning function instead of decorating
Missing the @ symbol for decorator
3. Given this Flask code, what will be the output when accessing /?
A. Missing parentheses in @app.before_request decorator
B. No return statement in before_request function causes error
C. No error; code works correctly
D. Route function must be named before_request
Solution
Step 1: Check before_request function requirements
The before_request function can run code and does not need to return anything unless it wants to stop the request.
Step 2: Verify decorator and route correctness
The decorator syntax is correct, and the route function is properly defined with a return statement.
Final Answer:
No error; code works correctly -> Option C
Quick Check:
before_request can have no return = B [OK]
Hint: before_request functions don't need to return unless blocking [OK]
Common Mistakes:
Expecting before_request to always return a response
Confusing decorator syntax with function call
Thinking route function must be named before_request
5. You want to use before_request to check if a user is logged in by verifying a session key. If not logged in, redirect to /login. Which code snippet correctly implements this?
hard
A. from flask import redirect, session
def check_login():
if 'user' not in session:
return redirect('/login')
app.before_request(check_login())
B. from flask import redirect, session
@app.before_request
def check_login():
if 'user' not in session:
return redirect('/login')
C. from flask import redirect, session
@app.before_request
def check_login():
if session['user'] is None:
return redirect('/login')
D. from flask import redirect
@app.before_request
def check_login():
if session.get('user') == False:
return redirect('/login')
Solution
Step 1: Check session key existence safely
from flask import redirect, session
@app.before_request
def check_login():
if 'user' not in session:
return redirect('/login') uses 'user' not in session which safely checks if the user is logged in.
Step 2: Confirm correct decorator and redirect usage
from flask import redirect, session
@app.before_request
def check_login():
if 'user' not in session:
return redirect('/login') uses the @app.before_request decorator and returns a redirect response correctly to stop further processing.
Final Answer:
from flask import redirect, session
@app.before_request
def check_login():
if 'user' not in session:
return redirect('/login') -> Option B
Quick Check:
Use @app.before_request and check 'user' in session = A [OK]
Hint: Use @app.before_request and check session key presence [OK]
Common Mistakes:
Calling the function instead of decorating
Checking session key with wrong condition
Not returning redirect to stop request
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.