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