0
0
Flaskframework~5 mins

Before_request hooks in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a before_request hook in Flask?
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
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.
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
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
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
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.