无法解析余数:' {{request.LANGUAGE_CODE}}'来自&{39; {{request.LANGUAGE_CODE}}'

时间:2017-10-19 03:52:41

标签: html django python-3.x

我使用django 1.11进行编程,现在我在post_list.html中有以下脚本



{% with lang={{request.LANGUAGE_CODE}} %}
{% endwith %}

           <p>
               {% if lang == 'zh-CN' %} {{object.category.name_zh_CN}} {% else %} {{object.category.name}} {% endif %}
           </p>
&#13;
&#13;
&#13;

但我实际上无法得到以下错误消息,请帮忙。谢谢。

&#13;
&#13;
TemplateSyntaxError at /adv/australia-strategic/
Could not parse the remainder: '{{request.LANGUAGE_CODE}}' from '{{request.LANGUAGE_CODE}}'
Request Method:	GET
Request URL:	http://localhost:8030/adv/australia-strategic/
Django Version:	1.11.6
Exception Type:	TemplateSyntaxError
Exception Value:	
Could not parse the remainder: '{{request.LANGUAGE_CODE}}' from '{{request.LANGUAGE_CODE}}'
Exception Location:	/Users/wzy/anaconda2/envs/python36/lib/python3.6/site-packages/django/template/base.py in __init__, line 700
Python Executable:	/Users/wzy/anaconda2/envs/python36/bin/python
Python Version:	3.6.3
Python Path:	
['/Users/wzy/expo3/expo',
 '/Users/wzy/anaconda2/envs/python36/lib/python36.zip',
 '/Users/wzy/anaconda2/envs/python36/lib/python3.6',
 '/Users/wzy/anaconda2/envs/python36/lib/python3.6/lib-dynload',
 '/Users/wzy/anaconda2/envs/python36/lib/python3.6/site-packages',
 '/Users/wzy/expo3/expo']
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

{% %}中不需要双花括号,而只需使用变量:

{% with lang=request.LANGUAGE_CODE %}
{% endwith %}
<p>
    {% if lang == 'zh-CN' %} {{object.category.name_zh_CN}} {% else %} {{object.category.name}} {% endif %}
</p>

您可以在django template guide

中详细了解相关信息

答案 1 :(得分:1)

{% with lang=request.LANGUAGE_CODE %}
    <p>
        {% if lang == 'zh-CN' %} {{object.category.name_zh_CN}} {% else %} {{object.category.name}} {% endif %}
    </p>
{% endwith %}

但你可以简化它:

<p>
    {% if request.LANGUAGE_CODE == 'zh-CN' %} {{object.category.name_zh_CN}} {% else %} {{object.category.name}} {% endif %}
</p>
相关问题