[英]How do I use 'is not None' in 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%}
但是,似乎沒有選擇“ not None”部分,因為如果用戶登錄但未輸入名字,則顯示如下:
! 你想去哪里?
如果我說:
firstname_of_logged_user is not None
它說一個錯誤:
if表達式末尾未使用的'is'。
似乎很簡單,但是卻行不通。 怎么了? 干杯!
您可以使用!=
運算符,而不是not
。 但是,可以進一步簡化以下內容:
{% if user.is_authenticated and firstname_of_logged_user %}
上面的代碼將檢查firstname_of_logged_user
在模板中是否為真。
自Django 1.10( 發行說明 )以來,if模板標記已支持is
運算符。
現在,您可以執行{% if x is None %}
和{% if x is not None %}
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.