2

I want to print something on the basis of the current language code. For that I did something like this:

{% if request.LANGUAGE_CODE == en %}
    <h1>English</h1>
{% endif %}

But this if condition does not compare the current language code. But if I print this {{request.LANGUAGE_CODE}} on the same page then it will print en as language code, but my if condition is not working and I don't know why ??

4

1 回答 1

6

LANGUAGE_CODE 是一个字符串,因此您只需像这样引用您的比较值:

{% if request.LANGUAGE_CODE == 'en' %}
    <h1>English</h1>
{% endif %}

还要检查ifequal 标签

{% ifequal request.LANGUAGE_CODE 'en' %}
    ...
{% endifequal %}

多一点:字符串上的 if 和 ifequal 区分大小写,因此您可能希望确保匹配正确的大小写(可能将 |lower 过滤器应用于两个参数)

于 2012-12-11T10:21:45.967 回答