在应用程序工厂中定义共享请求挂钩是否可以?
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
db.init_app(app)
csrf.init_app(app)
login_manager.init_app(app)
babel.init_app(app)
@app.before_request
def before_request_callback():
if request.view_args and 'locale' in request.view_args:
if request.view_args['locale'] not in app.config['SUPPORTED_LOCALES']:
return abort(404)
g.locale = request.view_args['locale']
request.view_args.pop('locale')
from . app_area__main import main as main_blueprint
app.register_blueprint(main_blueprint)
from . app_area__admin import admin as admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/admin')
答案 0 :(得分:1)
只需在主(app_area_main)蓝图中注册before_app_request
的函数即可。例如:
@main_blueprint.before_app_request
def before_app_request():
pass
所有传递给应用程序的请求都将调用该函数。
检查此link有关Flask中Blueprint的API的信息。