Method: flask.scaffold.Scaffold.before_request
Calls: 19, Exceptions: 0, Paths: 1Back
Path 1: 19 calls (1.0)
test_async_before_after_request.
test_async_before_after_request.
1@setupmethod
2 def before_request(self, f: T_before_request) -> T_before_request:
3 """Register a function to run before each request.
4
5 For example, this can be used to open a database connection, or
6 to load the logged in user from the session.
7
8 .. code-block:: python
9
10 @app.before_request
11 def load_user():
12 if "user_id" in session:
13 g.user = db.session.get(session["user_id"])
14
15 The function will be called without any arguments. If it returns
16 a non-``None`` value, the value is handled as if it was the
17 return value from the view, and further request handling is
18 stopped.
19
20 This is available on both app and blueprint objects. When used on an app, this
21 executes before every request. When used on a blueprint, this executes before
22 every request that the blueprint handles. To register with a blueprint and
23 execute before every request, use :meth:`.Blueprint.before_app_request`.
24 """
25 self.before_request_funcs.setdefault(None, []).append(f)
26 return f