假设我需要为每个烧瓶服务器执行before_request
如何在没有COPY-PASTE
@app.before_request
def before_request(*args, **kwargs):
params = get_params()
if params.has_key('start_dt') and params.has_key('end_dt'):
g.mongo_query = Mongo.get_date_range_query(params)
else:
g.mongo_query = {}
答案 0 :(得分:1)
您可以使用application factory。如果您初始化烧瓶应用程序如下:
sn -v yourprogram.exe
从管理/运行,您将
from flask import Flask
import yourdb
def create_app(config_filename):
app = Flask(__name__)
app.config.from_pyfile(config_filename)
yourdb.init_app(app)
#add_extensions
#add_blueprints/views
# ... some other configuration ...
@app.before_request
def before_request(*args, **kwargs):
#Your code
return app