每当我尝试在模板中使用过滤器时,都会出现TemplateSyntaxError

pan*_*smm 6 django django-templates

testlist只是一个对象列表.例如

testlist.0.name 
Run Code Online (Sandbox Code Playgroud)

简直就是"Test3"

我有一个文件temp.html

{% extends 'base.html' %}
{% block content %}
{{testlist.0.name | safe}}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

这就是temp.html文件中的所有内容,base.html与使用它的所有其他html文件一起使用

temp.html给了我

TemplateSyntaxError at /mytests/
Could not parse the remainder: ' | safe' from 'testlist.0.name | safe'
Request Method: GET
Request URL:    http://127.0.0.1:8000/mytests/
Django Version: 1.4
Exception Type: TemplateSyntaxError
Exception Value:    
Could not parse the remainder: ' | safe' from 'testlist.0.name | safe'
Run Code Online (Sandbox Code Playgroud)

当我把它改为:

{% extends 'base.html' %}
{% block content %}
{{testlist.0.lastedited |date:"SHORT_DATE_FORMAT" }} 
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

它给了我

TemplateSyntaxError at /mytests/
could not parse some characters: testlist.0.lastedited| ||date:"SHORT_DATE_FORMAT"
Request Method: GET
Request URL:    http://127.0.0.1:8000/mytests/
Django Version: 1.4
Exception Type: TemplateSyntaxError
Exception Value:    
Could not parse some characters: testlist.0.lastedited| ||date:"SHORT_DATE_FORMAT"
Run Code Online (Sandbox Code Playgroud)

你明白了.看来我只是不能在我的django模板中使用任何过滤器.我尝试了其他过滤器,仍然得到相同的东西.我错过了一些能够使用竖线字符的选项吗?是不是"|" 我的macbook pro上的键不是管道字符,而是django无法识别的其他字符?

not*_*peg 9

看起来你需要删除你testlist.0.lastedited和过滤器之间的空格.尝试类似的东西:

{% extends 'base.html' %}
{% block content %}
{{testlist.0.name|safe}}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

我猜这是你的问题,因为在文档中,它们没有任何空格,并且它们将模板解析为字符串或接近它的东西,因此空格可以影响它.

模板示例