I want to make my Flask Blueprint always run a method before executing any routes. Instead of decorating every route method in my blueprint with a custom decorator, I want to be able to do something like this:
def my_method():
do_stuff
section = Blueprint('section', __name__)
# Register my_method() as a setup method that runs before all routes
section.custom_setup_method(my_method())
@section.route('/two')
def route_one():
do_stuff
@section.route('/one')
def route_two():
do_stuff
Then basically both /section/one
and /section/two
will run my_method()
before executing code in route_one()
or route_two()
.
Is there a way to do this?
You can use the before_request decorator for blueprints. Like this:
@section.before_request
def my_method():
do_stuff
This automatically registers the function to run before any routes that belong to the blueprint.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments