MCPcopy
hub / github.com/pallets/flask / before_request

Method before_request

src/flask/sansio/scaffold.py:460–484  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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:

Callers

nothing calls this directly

Calls 1

setdefaultMethod · 0.80

Tested by

no test coverage detected