Django模板之模板標簽


標簽比變量更加復雜:一些在輸出中創建文本,一些通過循環或邏輯來控制流程,一些加載其后的變量將使用到的額外信息到模版中。

一些標簽需要開始和結束標簽 (例如:{% tag %} ...標簽 內容 ... {% endtag %}),有些標簽不需要結束{% tag %}

快捷鍵:輸入tag直接回車

 

常用模板標簽:for if with  [csrf_token  extends   include  block url load]見后續頁面

 

for標簽:循環遍歷可迭代變量中的每一個元素,沒有breakcontinue等復雜功能,相關操作類比python

1)遍歷列表:

tagtest.html模板:

{% for name in name_list %}

<li>{{ name }}</li>

{% endfor %}

    反向遍歷:

{% for name in name_list reversed %}

<li>{{ name }}</li>

{% endfor %} 

  views.py視圖:

def tagtest(request):

    list=["zhang","wang","li"]

    return render(request,"tagtest.html",{"name_list":list})

渲染結果:

  

2)列表中字典取值:

tagtest.html模板:

{% for info_dic in name_list %}

    <li>{{ info_dic.name }}</li>

{% endfor %}

views.py視圖:

def tagtest(request):

    list=[{"name":"le"},{"name":"yang"},{"name":"he"}]

    return render(request,"tagtest.html",{"name_list":list}) 

渲染結果:

 

             

      3)遍歷字典:

 

tagtest.html模板: 

{% for k,v in info_dict.items %} 

    <li>{{ k }}:{{ v }}</li> 

{% endfor %}

 

views.py視圖: 

def tagtest(request): 

dic={"name":"yang","age":20,"sex":"male"} 

return  render(request,"tagtest.html",{"info_dict":dic}) 

渲染結果:

 

             

      4forempty…:for遍歷一個空的變量或者未找到時執行empty 

  tagtest.html模板: 

   {% for info_dic in name_list %} 

      <li>{{ info_dic.name }}</li> 

  {% empty %} 

      <p>給出的變量為空或者未找到!</p> 

  {% endfor %} 

  views.py視圖: 

  def tagtest(request): 

      list=[] 

      return render(request,"tagtest.html",{"name_list":list}) 

  渲染結果:

 

             

        5forloop使用案例: 

tagtest.html模板: 

{% for i in %} 

<li>{{ forloop }}---{{ i }}</li> 

{% endfor %} 

   配合屬性使用: 

{% for i in %} 

<li>{{ forloop.counter }}---{{ i }}</li> 

{% endfor %} 

views.py視圖: 

def tagtest(request): 

    li=["python","mysql","web"] 

    return render(request,"tagtest.html",{"l":li})

           渲染結果:

         

注:循環序號可以通過{{forloop}}顯示,必須在循環內部用:

forloop.counter           

當前循環的索引值(1開始)forloop是循環器,通過點來使用功能

forloop.counter0         

 當前循環的索引值(從0開始)

forloop.revcounter        

當前循環的倒序索引值(從1開始)

forloop.revcounter0       

當前循環的倒序索引值(從0開始)

forloop.first             

當前循環是不是第一次循環(布爾值)

forloop.last              

當前循環是不是最后一次循環(布爾值)

forloop.parentloop        

本層循環的外層循環的對象,再通過上面的幾個屬性來顯示外層循環的計數等

 

 

if標簽:判斷變量的邏輯值是進行選擇性的輸出,類比python<  > = <= >= !=  ==  and or not not in is is not前后必須要有空格

tagtest.html模板:

{% if num > 100 %}

    <h1>大於100</h1>

{% elif num < 100 %}

    <h1>小於100</h1>

{% else %}

    <h1>等於100</h1>

{% endif %}

views.py視圖:

def tagtest(request):

    n=100

    return render(request,"tagtest.html",{"num":n})

渲染結果:

 

           

 

with標簽:多用於給一個復雜的變量起別名

注意:等號左右不要加空格。

{% with total=business.employees.count %}

    {{ total }} <!--只能在with語句體內用-->

{% endwith %}

 

{% with business.employees.count as total %}

    {{ total }}

{% endwith %}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM