Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 烧瓶中的应用程序范围请求挂钩。如何实施?_Python_Flask_Architecture - Fatal编程技术网

Python 烧瓶中的应用程序范围请求挂钩。如何实施?

Python 烧瓶中的应用程序范围请求挂钩。如何实施?,python,flask,architecture,Python,Flask,Architecture,在应用程序工厂内定义共享请求挂钩可以吗 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 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')        

只需在主(应用区域主)蓝图中的应用请求之前使用
注册一个函数即可。例如:

@main_blueprint.before_app_request
def before_app_request():
    pass
所有传递到应用程序的请求都将调用该函数


检查Flask中Blueprint的api。

只需在主(应用区域主)Blueprint中的应用请求之前使用
注册一个函数即可。例如:

@main_blueprint.before_app_request
def before_app_request():
    pass
所有传递到应用程序的请求都将调用该函数


检查一下烧瓶中Blueprint的api。

Blueprint
在请求之前也有
装饰器。@stamaimer你的意思是我应该在单独模块的蓝图中引用它/有一个共享的函数来装饰吗?
Blueprint
在请求之前也有
装饰器。@stamaimer你知道吗意思是说我应该在我的蓝图中从一个单独的模块中引用它/有一个共享的功能来装饰?嗯,这是否足够-只为main_蓝图注册它(忽略其他蓝图的注册)?文档中的“即使在蓝图之外”行是否意味着存在一次性注册?是的,因为装饰者的名字是
而不是.Hm,这是否足够-仅为主蓝图注册它(忽略其他蓝图的注册)?文档中的“即使在蓝图之外”行是否意味着存在一次性注册?是的,因为装饰者的名字是
before\u app\u request
而不是。