我浏览了文档,但找不到答案。这可能是我缺少的一些愚蠢的基本知识。我有带有金字塔的 jinja2 设置,并且正在使用 pyramid_jinja2 并在我的 .ini 文件中设置了我的过滤器:
jinja2.filters =
model_url = pyramid_jinja2.filters:model_url_filter
route_url = pyramid_jinja2.filters:route_url_filter
static_url = pyramid_jinja2.filters:static_url_filter
my_filter = myapp.view.my_filter
(my_filter 只是将每个值乘以 100)这很好用:
@view_config(renderer="json",route_name="tester")
def tester(request):
myList = [1,2,3,4]
return render_to_response("templates/test.jinja2",{'myList':myList},request=request)
test.jinja2:
{% for l in myList %}
{{ l | my_filter }}
{% endfor %}
但是,这不会:
@view_config(renderer="json",route_name="tester")
def tester(request):
myList = [1,2,3,4]
tmplate = Template(u'''\
{% for l in myList %}
{{ l | my_filter }}
{% endfor %}
''')
html = tmplate.render(myList = myList)
return render_to_response("templates/test.jinja2",{'html':html},request=request)
test.jinja2:
{{ html | safe }}
我的错误信息:
jinja2.exceptions.TemplateAssertionError
TemplateAssertionError: no filter named 'my_filter'