简体   繁体   中英

Flask doesn't call blueprint's before_request function

I want to add a before_request function to a blueprint. After registering the blueprint on the app, I decorate a function with before_request . However, the function is never called. Why doesn't it work?

__init__.py :

app = Flask(__name__)

from server.api import api
app.register_blueprint(api, url_prefix='/api')

@api.before_request
def check_if_connected():
    assert False, 'this is never printed'

api/__init__.py :

api = Blueprint('api', __name__)

Flask doesn't see what happens to the blueprint after it is registered. All setup, such as registering before request functions, must happen before registering the blueprint. Typically, things are registered near the blueprint's definition or in it's package, not after a semantically unrelated import.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM