日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python的Django框架中if標簽的相關使用

系統 2059 0

{% if %} 標簽檢查(evaluate)一個變量,如果這個變量為真(即,變量存在,非空,不是布爾值假),系統會顯示在 {% if %} 和 {% endif %} 之間的任何內容,例如:

            
{% if today_is_weekend %}
  
            

Welcome to the weekend!

{% endif %}

{% else %} 標簽是可選的:

            
{% if today_is_weekend %}
  
            

Welcome to the weekend!

{% else %}

Get back to work.

{% endif %}

Python 的“真值”

在Python和Django模板系統中,以下這些對象相當于布爾值的False

  • ??? 空列表([] )
  • ??? 空元組(() )
  • ??? 空字典({} )
  • ??? 空字符串('' )
  • ??? 零值(0 )
  • ??? 特殊對象None
  • ??? 對象False(很明顯)

??? 提示:你也可以在自定義的對象里定義他們的布爾值屬性(這個是python的高級用法)。

除以上幾點以外的所有東西都視為`` True``

{% if %} 標簽接受 and , or 或者 not 關鍵字來對多個變量做判斷 ,或者對變量取反( 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 %} 標簽不允許在同一個標簽中同時使用 and 和 or ,因為邏輯上可能模糊的,例如,如下示例是錯誤的: 比如這樣的代碼是不合法的:

            
{% if athlete_list and coach_list or cheerleader_list %}


          

系統不支持用圓括號來組合比較操作。 如果你確實需要用到圓括號來組合表達你的邏輯式,考慮將它移到模板之外處理,然后以模板變量的形式傳入結果吧。 或者,僅僅用嵌套的{% if %}標簽替換吧,就像這樣:

            
{% 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 %} 標簽, 請使用嵌套的`` {% if %}`` 標簽來達成同樣的效果:

            
{% if athlete_list %}
  
            

Here are the athletes: {{ athlete_list }}.

{% else %}

No athletes are available.

{% if coach_list %}

Here are the coaches: {{ coach_list }}.

{% endif %} {% endif %}

一定要用 {% endif %} 關閉每一個 {% if %} 標簽。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 淳化县| 伊川县| 绥棱县| 兴业县| 甘孜县| 二手房| 九江县| 什邡市| 安徽省| 武平县| 阜南县| 通山县| 应用必备| 高邑县| 德阳市| 宿松县| 琼结县| 米易县| 荃湾区| 龙川县| 洛扎县| 衡水市| 大田县| 英吉沙县| 登封市| 资源县| 新和县| 阳原县| 霍邱县| 依兰县| 富阳市| 福建省| 漳州市| 襄汾县| 车致| 新丰县| 于田县| 张家界市| 梅州市| 安达市| 巴塘县|