2017-10-19 56 views
0

我使用Django 1.11編程,現在我已經按照post_list.html無法解析餘數:從 '{{request.LANGUAGE_CODE}}' '{{request.LANGUAGE_CODE}}'

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

 
      <p> 
 
       {% if lang == 'zh-CN' %} {{object.category.name_zh_CN}} {% else %} {{object.category.name}} {% endif %} 
 
      </p>
腳本

但我不能得到朗實際上與以下錯誤信息,請help.Thanks。謝謝。

TemplateSyntaxError at /adv/australia-strategic/ 
 
Could not parse the remainder: '{{request.LANGUAGE_CODE}}' from '{{request.LANGUAGE_CODE}}' 
 
Request Method: \t GET 
 
Request URL: \t http://localhost:8030/adv/australia-strategic/ 
 
Django Version: \t 1.11.6 
 
Exception Type: \t TemplateSyntaxError 
 
Exception Value: \t 
 
Could not parse the remainder: '{{request.LANGUAGE_CODE}}' from '{{request.LANGUAGE_CODE}}' 
 
Exception Location: \t /Users/wzy/anaconda2/envs/python36/lib/python3.6/site-packages/django/template/base.py in __init__, line 700 
 
Python Executable: \t /Users/wzy/anaconda2/envs/python36/bin/python 
 
Python Version: \t 3.6.3 
 
Python Path: \t 
 
['/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']

回答

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閱讀更多關於這一點。

+0

'lang' VAR不會制定出'with' templatetag範圍 –

+0

感謝我完全固定你的好心幫助這個問題。 –

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> 
+0

感謝您的簡單替代解決方案,清除這麼多。 –