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?✗ Incorrect
The
@app.before_request decorator registers a function to run before each request.What happens if a
before_request function returns a response?✗ 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?✗ 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?✗ Incorrect
Flask runs
before_request functions in the order they were added.Can
before_request hooks access the request data?✗ 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.