我有一个使用jinja2模板过滤器的烧瓶应用程序.模板过滤器的示例如下:
@app.template_filter('int_date')
def format_datetime(date):
if date:
return utc_time.localize(date).astimezone(london_time).strftime('%Y-%m-%d %H:%M')
else:
return date
Run Code Online (Sandbox Code Playgroud)
如果我们在定义装饰器之前实例化应用程序,这可以正常工作,但是如果我们使用app工厂与flask-script管理器结合,那么我们就没有实例化的应用程序.对于例如:
def create_my_app(config=None):
app = Flask(__name__)
if config:
app.config.from_pyfile(config)
return app
manager = Manager(create_my_app)
manager.add_option("-c", "--config", dest="config", required=False)
@manager.command
def mycommand(app):
app.do_something()
Run Code Online (Sandbox Code Playgroud)
管理员接受实例化应用程序或应用程序工厂,因此乍一看似乎我们可以这样做:
app = create_my_app()
@app.template_filter('int_date')
....
manager = Manager(app)
Run Code Online (Sandbox Code Playgroud)
此解决方案的问题是管理器然后忽略该选项,因为在实例化期间已经配置了应用程序.那么有人应该如何将模板过滤器与flask-script扩展一起使用?