天天看點

django之六--模闆templates

一、前言

首先,我們要知道html是一門靜态語言,裡面沒法傳一些動态參數,也就是一個寫死的html頁面。

那麼,如果我們想實作在一個html頁面裡傳入不同的參數對應的參數值,這就可以用django架構提供的模闆傳參功能來解決。

二、模闆傳參

1、先在hello應用下或者【helloworld/hello/】下建立一個templates檔案夾,具體層級目錄如下。

django之六--模闆templates

2、在【helloworld/hello/templates】下建立一個【leiyuxing.html】,【leiyuxing.html】裡的模闆變量用{{變量名稱}}來表示。

django之六--模闆templates

3、修改【helloworld/helloworld/settings】下的一個常量【TEMPLATES】裡第一個下标值對應的值裡的的【DIRS】值為【[str(BASE_DIR)+"/hello/templates",]】。

django之六--模闆templates
django之六--模闆templates

4、在【helloworld/hello/views.py】裡新增一個視圖函數【leiyuxing】

django之六--模闆templates

點選context,可以看下源碼

django之六--模闆templates

5、在【helloworld/helloworld/urls.py】裡新增一個url比對規則【url(r"^xiaohong/$",views.leiyuxing)】

django之六--模闆templates

6、接着,啟動django項目【helloworld】的服務,在任一浏覽器上輸入位址【http://127.0.0.1:8000/xiaolei/】,可以得到正确的頁面資料。

django之六--模闆templates

三、django常用的模闆标簽

1、if/else标簽

條件判斷采用if/else标簽。

注意點:if語句最後一定要以endif結尾!

{% if condition1 %}

   執行這裡面的代碼塊A

{% elif condition2 %}

   執行這裡面的代碼塊B

{% else %}

   執行這裡面的代碼塊C

{% endif %}  # end表示該if語句結束。      

2、for标簽

與python語言裡的for語句的情形類似,for标簽的循環文法是【for X in Y】:Y是要疊代的序列,X是在每一個特定的循環中使用的變量名稱。

每一次循環中,django的html模闆渲染機制會渲染在 {% for %} 和 {% endfor %} 之間的所有内容。

注意點:for标簽最後一定要以endfor結尾!

<ul>

{% for athlete in athlete_list %}

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

{% endfor %}

</ul>      

3、 ifequal标簽

{% ifequal 變量A的變量值 變量B的變量值 %} 标簽用于比較兩個變量的變量值:當兩個變量的變量值相等時,執行在 {% ifequal %} 和 {% endifequal %} 之中所有的代碼塊。

注意點:ifequal标簽最後一定要以endifequal結尾!

舉個例子,用ifequal标簽來比較兩個模闆變量 user 和 currentuser :

{% ifequal user currentuser %}

    <h1>Welcome!</h1>

{% endifequal %}      

和 {% if %} 類似, {% ifequal %} 支援可選的 {% else%} 标簽。

舉個例子,用ifequal标簽來比較一個模闆變量 section 和一個資料類型為str的值"sitenews" :

{% ifequal section 'sitenews' %}

    <h1>Site News</h1>

{% else %}

    <h1>No News Here</h1>

{% endifequal %}      

4、ifnotequal标簽

{% ifnotequal 變量A的變量值 變量B的變量值 %} 标簽用于比較兩個變量的變量值:當兩個變量的變量值不相等時,執行在 {% ifequal %} 和 {% endifequal %} 之中所有的代碼塊。

注意點:ifnotequal标簽最後一定要以endifnotequal結尾!

舉個例子,用ifnotequal标簽來比較兩個模闆變量 user 和 currentuser :

{% ifnotequal user currentuser %}

    <h1>Welcome!</h1>

{% endifnotequal %}      

和 {% if %} 類似, {% ifnotequal %} 支援可選的 {% else%} 标簽。

舉個例子,用ifnotequal标簽來比較一個模闆變量 section 和一個資料類型為str的值"sitenews" :

{% ifnotequal section 'sitenews' %}

    <h1>Site News</h1>

{% else %}

    <h1>No News Here</h1>

{% endifnotequal %}      

5、 注釋标簽

django的html模闆的注釋标簽,是使用{#  #}。

6、其餘常用标簽

其餘常用django模闆标簽的使用,可以參考該菜鳥教程:https://www.runoob.com/django/django-template.html。

這些内容直接用截圖的形式來記錄,不多做文字上面的分析了。

具體知識點,都可以參考該菜鳥教程:https://www.runoob.com/django/django-template.html。

django之六--模闆templates
django之六--模闆templates
django之六--模闆templates
django之六--模闆templates
django之六--模闆templates

7、關于模闆标簽的具體使用

目前隻用截圖的形式來記錄調試過程的相關重要細節,讓大家對模闆标簽的具體使用有個初步印象。

django之六--模闆templates
django之六--模闆templates
django之六--模闆templates
django之六--模闆templates
django之六--模闆templates
django之六--模闆templates
django之六--模闆templates
django之六--模闆templates

四、django常用的過濾器

django常用的過濾器,也仍然可以參考該菜鳥教程:https://www.runoob.com/django/django-template.html。