简体   繁体   English

Flask不调用蓝图的before_request函数

[英]Flask doesn't call blueprint's before_request function

I want to add a before_request function to a blueprint. 我想在图纸中添加一个before_request函数。 After registering the blueprint on the app, I decorate a function with before_request . 在应用程序上注册了蓝图之后,我用before_request装饰了一个函数。 However, the function is never called. 但是,永远不会调用该函数。 Why doesn't it work? 为什么不起作用?

__init__.py : __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/__init__.py

api = Blueprint('api', __name__)

Flask doesn't see what happens to the blueprint after it is registered. 注册后,Flask看不到蓝图发生了什么。 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. 通常,事物是在蓝图的定义附近或在软件包中注册的,而不是在语义上不相关的导入之后注册的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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