如何在Django模板中使用“不是不是”?

Sei*_*eif 0 python django django-templates

我要自定义主页上的问候语,因此它是:

登录并输入名字时:

约翰!你想去哪里?

其他:

你想去哪里?

这是我的模板:

 {% if user.is_authenticated and not firstname_of_logged_user == None%}
 <h1 class="text-center" id="extraglow">{{firstname_of_logged_user}}! where do 
 you want to go?</h1></label>
{%else%}
<h1 class="text-center" id="extraglow">where do you want to go?</h1></label>
                                {%endif%}
Run Code Online (Sandbox Code Playgroud)

但是,似乎没有选择“ not None”部分,因为如果用户登录但未输入名字,则显示如下:

!你想去哪里?

如果我说:

firstname_of_logged_user is not None
Run Code Online (Sandbox Code Playgroud)

它说一个错误:

if表达式末尾未使用的'is'。

似乎很简单,但是却行不通。怎么了?干杯!

Ala*_*air 7

is自Django 1.10(发行说明)以来,if模板标记已支持该运算符。

您现在可以执行{% if x is None %}{% if x is not None %}


rne*_*ius 6

您可以使用!=运算符代替not。但是,可以进一步简化以下内容:

{% if user.is_authenticated and firstname_of_logged_user %}
Run Code Online (Sandbox Code Playgroud)

上面的内容将检查firstname_of_logged_user您模板中的真实性。