前言:Web框架的模式css
Django的模板語言html
變量python
在Django的模板語言中語言使用: {{ 變量名}}數據庫
點(.) 在模板語言中一樣是萬能的django
{# 取l中的第一個參數 #} {{ l.0 }} {# 取字典中key的值 #} {{ d.name }} {# 取對象的name屬性 #} {{ person_list.0.name }} {# .操做只能調用不帶參數的方法 #} {{ person_list.0.dream }}
{% tag %}
for標籤 循環的序號能夠經過{{forloop}}顯示bootstrap
<h3>循環取值1</h3><hr> {% for item in person_list %} <p>{{ item.name }},{{ item.age }}</p> {% endfor %} <h3>循環取值2:倒序</h3><hr> {% for item in person_list reversed %} <!--序號從1開始--> <p>{{ forloop.counter }}----->{{ item.name }},{{ item.age }}</p> <!--序號從0開始--><p>{{ forloop.counter0 }}----->{{ item.name }},{{ item.age }}</p><!-- 序號倒序 --><p>{{ forloop.revcounter }}----->{{ item.name }},{{ item.age }}</p> {% endfor %} <h3>循環取值3:字典</h3><hr> {% for k,v in d.items %} <p>{{ k }},{{ v}}</p> {% endfor %}
for循環中的一些可用參數安全
Variable | Description |
---|---|
forloop.counter |
當前循環的索引值(從1開始) |
forloop.counter0 |
當前循環的索引值(從0開始) |
forloop.revcounter |
當前循環的倒序索引值(從1開始) |
forloop.revcounter0 |
當前循環的倒序索引值(從0開始) |
forloop.first |
當前循環是否是第一次循環(布爾值) |
forloop.last |
當前循環是否是最後一次循環(布爾值) |
forloop.parentloop |
本層循環的外層循環 |
for...empty:for 標籤帶有一個可選的{% empty %}從句,以便在給出的組爲空或者沒有找到的時,添加操做網絡
{% for person in person_list %} <p>{{ person.name }}</p> {% empty %} <p>sorry,no person here</p> {% endfor %}
if標籤 {% if 條件 %} {{ 執行語句 }} {% endif %}app
if語句支持and,or,==,<,>, <=, >=, in , not in, is, is not判斷框架
{% if i > 300 %} <p>大於{{ i }}</p> {% elif i == 200 %} <p>等於{{ i }}</p> {% else %} <p>小於{{ i }}</p> {% endif %}
with 語句 定義一箇中間變量
with 上下文管理器協議
應用場景:網絡鏈接,數據庫鏈接,文件句柄,用到鎖
{% with total=business.employees.count %} {{ total }} employee{{ total|pluralize }} {% endwith %}
{% with file_size as fs %} <h1>{{ fs }}</h1> {% endwith %}
csrf_token 這個標籤用於跨站請求僞造保護
1, default: 若是一個變量是false或者爲空,使用給定的默認值。不然,使用變量值
<p>default過濾器:{{ li|default:"若是顯示爲空,設置的解釋性的內容" }}</p>
2, length:返回值的長度,對字符串和列表,字典都起做用
<h1>{{ list1|length }}</h1> <h1>{{ dic|length }}</h1> #若是 list1 是 ['a', 'b', 'c', 'd'],那麼輸出是 4
3, filesizeformat: 將值格式化爲一個「人類可讀的」文件尺寸
<h1>{{ value|filesizeformat }}</h1> #若是value是12345678 ,輸出將會是117.7MB
4, date: 若是 value= datetime.now()
<h1>{{ time|date:"Y-m-d H:i:s" }}</h1> #導入模塊 from datetime import datetime # time = datetime.now() #輸出爲 當前時間 2018-11-01 17:57:45
5, slice: 切片
<h1>{{ num|slice:"0:5" }}</h1> #num = "hello world」 輸出爲hello,一樣遵循顧頭不顧尾
6,truncatechars 截斷
若是字符串子多於指定的字符數量,那麼會被截斷,截斷的字符串將以可翻譯的省略號序列(...)結尾
<h1>截斷字符{{ num|truncatechars:5 }}</h1> #num = "hello world" 輸出爲 he... <h1>截斷單詞{{ num|truncatewords:5 }}</h1> #單詞是以空格進行區分,num="h e l l o w o r l d" 輸出爲 h e l l o ...
7, safe 爲了保證html在某些狀況下不被轉義,就使用safe。
<h1>{{ a }}</h1> # a = "<a href='https://www.baidu.com'>百度</a>" 輸出爲字符串 <h1>{{ a|safe }}</h1> #當連接是安全的,則是一個連接標籤
8,cut 移除value中全部與給出的變量相同的字符串
<h1>{{ num|cut:"l" }}</h1> #num = "hello world" 輸出爲 heo word
9, join 使用字符串鏈接列表,與 Python中的 str.join(list)相同
<h1>{{ list3|join:"-" }}</h1> #list3 = ["h", "e", 'l', 'l', 'o'] 輸出爲 h-e-l-l-o
10, timesince 將日期格式設爲日期起的時間,採用一個可選參數,
<h1>{{ hours|timesince }}</h1> # hours = datetime.now() - timedelta(hours=4) 輸出爲 4 hours
自定義filter
5, 使用自定義的filter
{% load new_filter %} <p>{{ name|new_filter }}</p> #name = "alex" 輸出爲 alexsb
在html中導入剛纔建立的文件
重啓Django項目
按照普通filter調用自定義的函數
什麼狀況下使用母版
使用
在母版中定義須要被替換的block
{% block page-css %} {% endblock %} {% block page-main %} {% endblock %} {% block page-js %} {% endblock %}
在子頁中
先繼承母版
{% extends 「母版的html" %} {% block page-main %} 正文 {% endblock %}
組件
當頁面上相對獨立的某個部分能夠單獨拿出來放到一個單獨的html文件中
語法
{% inclue '組件.html' %}
靜態文件相關
將靜態文件的路勁改爲動態拼接,避免在html頁面中應編碼靜態文件導入路勁
倆種用法
{% load static %} # 第一種 經常使用 <script src="{% static 'bootstrap-3.3.7/js/bootstrap.js' %}"></script> # 第二種 <link href="{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css">
自定義simpletag
自定義的inclusion_tag