是否可以将一个Python模块导入Jinja模板,以便我可以使用它的功能?将Python模块导入Jinja模板?
例如,我有一个format.py文件,其中包含格式化日期和时间的方法。在金佳宏中,我可以做些什么像以下?
{% from 'dates/format.py' import timesince %}
{% macro time(mytime) %}
<a title="{{ mytime }}">{{ timesince(mytime) }}</a>
{% endmacro %}
因为format.py不是一个模板,上面的代码给了我这个错误:
UndefinedError: the template 'dates/format.py' (imported on line 2 in 'dates/macros.html') does not export the requested name 'timesince'
...但我不知道是否有另一种方式来实现这一目标。