Register a function to run before each request. For example, this can be used to open a database connection, or to load the logged in user from the session. .. code-block:: python @app.before_request def load_user(): if "user_id" in
(self, f: T_before_request)
| 458 | |
| 459 | @setupmethod |
| 460 | def before_request(self, f: T_before_request) -> T_before_request: |
| 461 | """Register a function to run before each request. |
| 462 | |
| 463 | For example, this can be used to open a database connection, or |
| 464 | to load the logged in user from the session. |
| 465 | |
| 466 | .. code-block:: python |
| 467 | |
| 468 | @app.before_request |
| 469 | def load_user(): |
| 470 | if "user_id" in session: |
| 471 | g.user = db.session.get(session["user_id"]) |
| 472 | |
| 473 | The function will be called without any arguments. If it returns |
| 474 | a non-``None`` value, the value is handled as if it was the |
| 475 | return value from the view, and further request handling is |
| 476 | stopped. |
| 477 | |
| 478 | This is available on both app and blueprint objects. When used on an app, this |
| 479 | executes before every request. When used on a blueprint, this executes before |
| 480 | every request that the blueprint handles. To register with a blueprint and |
| 481 | execute before every request, use :meth:`.Blueprint.before_app_request`. |
| 482 | """ |
| 483 | self.before_request_funcs.setdefault(None, []).append(f) |
| 484 | return f |
| 485 | |
| 486 | @setupmethod |
| 487 | def after_request(self, f: T_after_request) -> T_after_request: |
nothing calls this directly
no test coverage detected