在模板中,否,您不能导入python代码。
这样做的方法是将函数注册为jinja2定制过滤器,如下所示:
在您的python文件中:
from dates.format import timesinceenvironment = jinja2.Environment(whatever)environment.filters['timesince'] = timesince# render template here
在您的模板中:
{% macro time(mytime) %}<a title="{{ mytime }}">{{ mytime|timesince }}</a>{% endmacro %}


