我有一个使用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
如果我们在定义装饰器之前实例化应用程序,这可以正常工作,但是如果我们使用app工厂与flask-script管理器结合,那么我们就没有实例化的应用程序。对于example:
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()
管理员接受实例化的应用程序或应用工厂,所以乍一看似乎我们可以这样做:
app = create_my_app()
@app.template_filter('int_date')
....
manager = Manager(app)
此解决方案的问题是管理器然后忽略该选项,因为在实例化期间已经配置了应用程序。那么有人应该如何将模板过滤器与flask-script扩展一起使用?
答案 0 :(得分:8)
这是蓝图发挥作用的地方。我会定义一个蓝图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
有关此方法的最小工作示例,请参阅:https://github.com/iiSeymour/app_factory
答案 1 :(得分:1)
可以找到解决方案here,它表明它们可以通过两种方式定义jinja模板过滤器。因此,不是在工厂外定义装饰器,而是可以修改jinja_env。这可以在app工厂中完成,例如:
def format_datetime(date):
if date:
return utc_time.localize(date).astimezone(london_time).strftime('%Y-%m-%d %H:%M')
else:
return date
def create_app(production=False):
app = Flask(__name__)
....
# Register Jinja2 filters
app.jinja_env.filters['datetime'] = format_datetime
manager = Manager(create_app)
...