2017-10-19 3 views
0

I avec django 1.11 programmant, maintenant je le script suivant dans post_list.htmlImpossible d'analyser le reste: {{request.LANGUAGE_CODE}} 'de '{{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>

mais je ne peux pas obtenir réellement avec le message d'erreur suivant, s'il vous plaît 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']

Répondre

1

Les doubles accolades ne sont pas nécessaires dans le {% %}, au contraire, vous pouvez simplement utiliser la variable:

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

Vous pouvez en lire plus à ce sujet dans le django template guide.

+0

'lang' var ne fonctionnera pas hors de portée' de with' de templatetag –

+0

grâce Je suis entièrement résolu ce problème avec votre aide bien voulu. –

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

Mais vous pouvez simplifier:

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

Merci pour votre solution alternative simple, effacez beaucoup. –