Python的Django框架中if標(biāo)簽的相關(guān)使用
{% if %} 標(biāo)簽檢查(evaluate)一個變量,如果這個變量為真(即,變量存在,非空,不是布爾值假),系統(tǒng)會顯示在 {% if %} 和 {% endif %} 之間的任何內(nèi)容,例如:
{% if today_is_weekend %} <p>Welcome to the weekend!</p> {% endif %}
{% else %} 標(biāo)簽是可選的:
{% if today_is_weekend %} <p>Welcome to the weekend!</p> {% else %} <p>Get back to work.</p> {% endif %}
Python 的“真值”
在Python和Django模板系統(tǒng)中,以下這些對象相當(dāng)于布爾值的False
- 空列表([] )
- 空元組(() )
- 空字典({} )
- 空字符串('' )
- 零值(0 )
- 特殊對象None
- 對象False(很明顯)
提示:你也可以在自定義的對象里定義他們的布爾值屬性(這個是python的高級用法)。
除以上幾點(diǎn)以外的所有東西都視為`` True``
{% if %} 標(biāo)簽接受 and , or 或者 not 關(guān)鍵字來對多個變量做判斷 ,或者對變量取反( not ),例如: 例如:
{% if athlete_list and coach_list %} Both athletes and coaches are available. {% endif %} {% if not athlete_list %} There are no athletes. {% endif %} {% if athlete_list or coach_list %} There are some athletes or some coaches. {% endif %} {% if not athlete_list or coach_list %} There are no athletes or there are some coaches. {% endif %} {% if athlete_list and not coach_list %} There are some athletes and absolutely no coaches. {% endif %}
{% if %} 標(biāo)簽不允許在同一個標(biāo)簽中同時使用 and 和 or ,因?yàn)檫壿嬌峡赡苣:?,例如,如下示例是錯誤的: 比如這樣的代碼是不合法的:
{% if athlete_list and coach_list or cheerleader_list %}
系統(tǒng)不支持用圓括號來組合比較操作。 如果你確實(shí)需要用到圓括號來組合表達(dá)你的邏輯式,考慮將它移到模板之外處理,然后以模板變量的形式傳入結(jié)果吧。 或者,僅僅用嵌套的{% if %}標(biāo)簽替換吧,就像這樣:
{% if athlete_list %} {% if coach_list or cheerleader_list %} We have athletes, and either coaches or cheerleaders! {% endif %} {% endif %}
多次使用同一個邏輯操作符是沒有問題的,但是我們不能把不同的操作符組合起來。 例如,這是合法的:
{% if athlete_list or coach_list or parent_list or teacher_list %}
并沒有 {% elif %} 標(biāo)簽, 請使用嵌套的`` {% if %}`` 標(biāo)簽來達(dá)成同樣的效果:
{% if athlete_list %} <p>Here are the athletes: {{ athlete_list }}.</p> {% else %} <p>No athletes are available.</p> {% if coach_list %} <p>Here are the coaches: {{ coach_list }}.</p> {% endif %} {% endif %}
一定要用 {% endif %} 關(guān)閉每一個 {% if %} 標(biāo)簽。
相關(guān)文章
Python數(shù)據(jù)分析之缺失值檢測與處理詳解
在實(shí)際的數(shù)據(jù)處理中,缺失值是普遍存在的,如何使用 Python 檢測和處理缺失值,就是本文要講的主要內(nèi)容。感興趣的同學(xué)可以關(guān)注一下2021-12-12詳解Django中CBV(Class Base Views)模型源碼分析
這篇文章主要介紹了詳解Django中CBV(Class Base Views)模型源碼分析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02Python UnicodeEncodeError: ''gbk'' codec can''t encode chara
這篇文章主要介紹了Python UnicodeEncodeError: 'gbk' codec can't encode character 解決方法,需要的朋友可以參考下2015-04-04python實(shí)現(xiàn)圖像處理之PiL依賴庫的案例應(yīng)用詳解
這篇文章主要介紹了python實(shí)現(xiàn)圖像處理之PiL依賴庫的案例應(yīng)用詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07利用Celery實(shí)現(xiàn)Django博客PV統(tǒng)計(jì)功能詳解
給網(wǎng)站增加pv、uv統(tǒng)計(jì),可以是件很簡單的事,也可以是件很復(fù)雜的事。下面這篇文章主要給大家介紹了利用Celery實(shí)現(xiàn)Django博客PV統(tǒng)計(jì)功能的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05python+influxdb+shell編寫區(qū)域網(wǎng)絡(luò)狀況表
這篇文章主要為大家詳細(xì)介紹了python+influxdb+shell編寫區(qū)域網(wǎng)絡(luò)狀況表,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Python實(shí)現(xiàn)string字符串連接的方法總結(jié)【8種方式】
這篇文章主要介紹了Python實(shí)現(xiàn)string字符串連接的方法,結(jié)合實(shí)例形式總結(jié)分析了Python實(shí)現(xiàn)字符串連接的8種常見操作技巧,需要的朋友可以參考下2018-07-07