A before_request hook is a function that runs before each request is processed. It lets you run code like checking user login or setting up data before the main view runs.
Click to reveal answer
beginner
How do you register a before_request function in Flask?
You use the @app.before_request decorator above a function. Flask calls this function before every request automatically.
Click to reveal answer
intermediate
Can a before_request function stop a request from continuing?
Yes. If the before_request function returns a response (like a redirect or error), Flask stops and sends that response immediately, skipping the view function.
Click to reveal answer
beginner
Why use before_request hooks instead of putting code inside each view?
They help avoid repeating code in every view. For example, checking if a user is logged in or setting up common data can be done once in a before_request hook.
Click to reveal answer
intermediate
What happens if multiple before_request functions are registered?
Flask runs all before_request functions in the order they were registered. If any returns a response, the rest are skipped.
Click to reveal answer
What decorator do you use to create a before_request hook in Flask?
A@app.after_request
B@app.teardown_request
C@app.route
D@app.before_request
✗ Incorrect
The @app.before_request decorator registers a function to run before each request.
What happens if a before_request function returns a response?
AThe response is sent immediately, skipping the view function.
BThe response is ignored and the view runs anyway.
CFlask raises an error.
DThe response is logged but the view runs.
✗ Incorrect
Returning a response in before_request stops further processing and sends that response.
Which of these is a common use for before_request hooks?
AServing static files
BRendering HTML templates
CChecking if a user is logged in
DHandling form submissions
✗ Incorrect
before_request hooks are often used to check user login status before views run.
If you register multiple before_request functions, in what order do they run?
AIn reverse order
BIn the order they were registered
CRandom order
DAlphabetical order by function name
✗ Incorrect
Flask runs before_request functions in the order they were added.
Can before_request hooks access the request data?
AYes, they can access request data like headers and form inputs
BNo, request data is not available yet
COnly URL parameters are accessible
DOnly after the view runs
✗ Incorrect
before_request hooks run after Flask receives the request, so they can access all request data.
Explain what a before_request hook is and how it affects the request flow in Flask.
Think about what happens before your page loads.
You got /4 concepts.
Describe a practical example where using a before_request hook improves your Flask app.
Imagine you want to protect many pages from unauthorized access.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a before_request hook in Flask?
easy
A. To run code before every request to prepare or check something
B. To define the main route of the application
C. To handle errors after a request is processed
D. To send the final response to the client
Solution
Step 1: Understand the role of before_request
The before_request hook runs before each request to prepare data or check conditions.
Step 2: Compare with other options
Defining routes, handling errors, or sending responses happen elsewhere, not in before_request.
Final Answer:
To run code before every request to prepare or check something -> Option A
Quick Check:
before_request runs before request = C [OK]
Hint: before_request runs code before each request [OK]
Common Mistakes:
Confusing before_request with route handlers
Thinking it handles errors or sends responses
Assuming it runs after the request
2. Which of the following is the correct way to register a before_request hook in Flask that does not short-circuit the request?
easy
A. @app.before_request\nclass BeforeHook: pass
B. @app.before_request()\ndef before(): pass
C. @app.before_request\ndef before(): return 'Hello'
D. @app.before_request\ndef before(): pass
Solution
Step 1: Recall the decorator syntax
The before_request decorator is used without parentheses: @app.before_request.
Step 2: Check function definition
The hook must be a function, not a class, and must not return a response to continue to the route.
Final Answer:
@app.before_request\ndef before(): pass -> Option D
Quick Check:
Decorator without () and function = D [OK]
Hint: Use @app.before_request without parentheses [OK]
Common Mistakes:
Adding parentheses after @app.before_request
Defining a class instead of a function
Returning a value when not needed
3. Given this Flask code, what will be the output when accessing /?
D. Returning None in before_request causes an error
Solution
Step 1: Understand return behavior in before_request
Returning None means no response is sent, so Flask continues to route handler.
Step 2: Check if printing is allowed
Printing is allowed and useful for debugging; it does not cause errors.
Final Answer:
Nothing is wrong; code works fine -> Option B
Quick Check:
None return means continue normal route = A [OK]
Hint: Return None or nothing to continue request normally [OK]
Common Mistakes:
Thinking return None causes error
Believing before_request must always return response
Assuming print statements break Flask hooks
5. You want to use a before_request hook to block access to all routes except /login unless a user is logged in. Which code snippet correctly implements this?
hard
A. from flask import request, redirect, session
@app.before_request
def block():
if request.path != '/login' and not session.get('user'):
return redirect('/login')
B. from flask import request, redirect
@app.before_request
def block():
if request.path == '/login':
return redirect('/login')
C. from flask import session
@app.before_request
def block():
if not session.get('user'):
return 'Access Denied'
D. from flask import request
@app.before_request
def block():
if request.path == '/login' or session.get('user'):
return redirect('/login')
Solution
Step 1: Identify the condition to block access
We want to redirect to /login if the path is not /login and user is not logged in.
Step 2: Check each option logic
from flask import request, redirect, session
@app.before_request
def block():
if request.path != '/login' and not session.get('user'):
return redirect('/login') correctly checks path and session, redirecting only when needed. Others either redirect wrongly or deny access without redirect.
Final Answer:
from flask import request, redirect, session
@app.before_request
def block():
if request.path != '/login' and not session.get('user'):
return redirect('/login') -> Option A
Quick Check:
Check path and session, redirect if not logged in = A [OK]
Hint: Check path and session, redirect if not logged in [OK]
Common Mistakes:
Redirecting /login to itself causing loop
Not checking path before redirect
Returning string instead of redirect response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.