6

testlistは、単なるオブジェクトのリストです。例えば

testlist.0.name 

単に「Test3」です

私はファイルtemp.htmlを持っています

{% extends 'base.html' %}
{% block content %}
{{testlist.0.name | safe}}
{% endblock %}

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'

次のように変更すると:

{% extends 'base.html' %}
{% block content %}
{{testlist.0.lastedited |date:"SHORT_DATE_FORMAT" }} 
{% endblock %}

それは私に与えます

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"

あなたはアイデアを得る。djangoテンプレートでフィルターを使用できないようです。私は他のフィルターを試しましたが、それでも同じものが得られます。パイプ文字の使用を可能にするいくつかのオプションがありませんか?「|」ということでしょうか 私のmacbookproのキーはパイプ文字ではなく、djangoが認識できない他の文字ですか?

4

1 に答える 1

9

testlist.0.lasteditedとフィルターの間の空白を削除する必要があるようです。次のようなものを試してください:

{% extends 'base.html' %}
{% block content %}
{{testlist.0.name|safe}}
{% endblock %}

これがあなたの問題だと思います。ドキュメントには空白がなく、テンプレートを文字列またはそれに近いものとして解析するため、空白が影響を与える可能性があります。

テンプレートの例

于 2012-07-01T16:59:17.250 に答える