我有一个使用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扩展一起使用?
这是蓝图发挥作用的地方.我会定义一个蓝图,core然后把我所有的自定义模板过滤器说出来core/filters.py.
要在使用蓝图时将过滤器注册到烧瓶中的应用程序,您需要使用app_template_filter而不是template_filter.这样,您仍然可以使用装饰器模式来注册过滤器并使用应用程序工厂方法.
使用蓝图的应用程序的典型目录布局可能如下所示:
??? app
? ??? blog
? ? ??? __init__.py # blog blueprint instance
? ? ??? routes.py # core filters can be used here
? ??? core
? ? ??? __init__.py # core blueprint instance
? ? ??? filters.py # define filters here
? ? ??? routes.py # any core views are defined here
? ??? __init__.py # create_app is defined here & blueprint registered
??? manage.py # application is configured and created here
Run Code Online (Sandbox Code Playgroud)
有关此方法的最小工作示例,请参阅:https://github.com/iiSeymour/app_factory
| 归档时间: |
|
| 查看次数: |
489 次 |
| 最近记录: |