1. 程式人生 > >django框架之模板系統2

django框架之模板系統2

title 超過 com tab var mvc 文本 top table

昨日內容回顧:

  1. MVC和MTV框架
MVC
M: model 模型 存寫數據
V: view 視圖 給用戶展示頁面
C:controller 控制器 負責調度 傳遞指令
MTV
M:model 模型 ORM操作
T:template 模板 返回HTML頁面
V: view 視圖 負責主要的業務邏輯
2. 模板:
1. 變量
{{ 變量名 }}

2. .用法
{{ name_list.0 }}
{{ name_dict.key }}
{{ p1.name }}
{{ p1.dream }}
{{ name_dict.items }}
{{ name_dict.keys }}
3. filters 過濾器
1. 語法
{{ 變量|filter:參數 }}
2. 內置的filter
default:‘noting‘
filesizeformat 顯示文件大小 111111 ——》 kb mb pb
add:‘1‘ 數字加法 字符串拼接 列表的拼接
lower 小寫
upper 大寫
title 首字母大寫
rjust:20 右對齊
ljust:20 左對齊
center:20 居中
length 返回字符串或者列表的長度
slice:"::" 切片
first 取第一個
last 取最後一個
join:‘//‘ 將列表拼接成字符串
truncatechars:18 將超過指定長度的內容替換成...
date:‘Y-m-d H:i:s‘ 2018-7-31 8:52:59
safe 對你的HTML文本不進行轉義
3. 自定義filter
1. 在app01下創建一個叫templatetags的Python包
2. 在templatetags的文件夾下創建py文件 myfilters
3. 在py文件中寫代碼
from django import template
register = template.Library()

@register.filter
def add_sb(value,arg=‘aaa‘):
return "{}_sb_{}".formart(value,arg)

@register.filter(name=‘sb‘)
def add_sb(value,arg=‘aaa‘):
return "{}_sb_{}".formart(value,arg)

4. 使用自定義filter
{% load myfilters %}
{{ name|add_sb:‘xxx‘}}
{{ name|sb:‘xxx‘}}

今日內容:

Tags

for循環:

  

VariableDescription
forloop.counter 當前循環的索引值(從1開始)
forloop.counter0 當前循環的索引值(從0開始)
forloop.revcounter 當前循環的倒序索引值(從1開始)
forloop.revcounter0 當前循環的倒序索引值(從0開始)
forloop.first 當前循環是不是第一次循環(布爾值)
forloop.last 當前循環是不是最後一次循環(布爾值)
forloop.parentloop 本層循環的外層循環

<ul>
    {
% for name in name_list %} <li>{{ forloop }}</li> <li>{{ forloop.counter0 }}-{{ name }} first:{{ forloop.first }}last:{{ forloop.last }}</li> {% endfor %} </ul> <ul> {% for namelist in name_list2 %} {% for name in namelist %} <li>{{ forloop.parentloop }}_{{ name }}</li> {
% endfor %} {% endfor %} </ul>

下面這個例子更好的解釋parentloop的用法

<table border="1">
    {% for namelist in name_list2 %}
        <tr>
            {% for name in namelist %}
                {% if forloop.parentloop.counter|divisibleby:2 %}
                    <td style="color: red">{{ name }}</td>
                    {% else %}
                    <td>{{ name }}</td>
                {% endif %}
                {% endfor %}
        </tr>
    {% endfor %}
</table>

for ... empty:

如果變量名不存在,可以用empty來代替。

<ul>
    {% for i in asd %}
    <li>{{ name }}</li>
        {% empty %}
        <li>空空如也</li>
    {% endfor %}
</ul>

if,elif和else

{% if p1.age < 73 %}
    還沒遇到坎
{% elif p1.age > 84 %}
    過了兩個坎,還有更多坎
{% else %}
    剛過一個坎,就快涼了
{% endif %}

{% if name_list|length > 3 %}
    住豪華別墅
{% else %}
    住橋洞
{% endif %}

if語句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判斷。

with

定義一個中間變量

{% with p1.dream as dream %}

    {{ dream }}
    {{ dream }}
    {{ dream }}
    {{ dream }}
    {{ dream }}
    {{ dream }}

{% endwith %}

csrf_token:

這個標簽用於跨站請求偽造保護。

在頁面的form表單裏面寫上{% csrf_token %}

之前在提交post請求時我們是註釋了csrf的中間間註釋掉了,但終歸不能這樣,所以在form表單裏面寫上{% csrf_token %}就可以不用註釋那個中間件了。

註意事項

1. Django的模板語言不支持連續判斷,即不支持以下寫法:

{% if a > b > c %}
...
{% endif %}

2. Django的模板語言中屬性的優先級大於方法

def xx(request):
    d = {"a": 1, "b": 2, "c": 3, "items": "100"}
    return render(request, "xx.html", {"data": d})

如上,我們在使用render方法渲染一個頁面的時候,傳的字典d有一個key是items並且還有默認的 d.items() 方法,此時在模板語言中:

{{ data.items }}

默認會取d的items key的值。

母板

我們在寫html的時候,會有很多重復的東西,就比如說圖書館管理系統,上面的導航欄和左側欄每個頁面都是一樣的,所以我們可以把他們做成一個母版,在在寫頁面的時候,直接引入就可以了。

我們先將一樣的代碼放在一個base的模板html裏,然後再另一個html裏引入這個模板{% extends ‘base.html‘ %},然後再模板html中不一樣需要修改的地方定義一個{%block page-main%}

                                

                              {% endblock%}  

用的時候在另一個html裏寫{%block page-main%}

              不一樣的內容

            {%endblock%} 這個html裏的block中的內容會覆蓋掉母版中block的內容

舉個例子:

母版代碼:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Title</title>
  {% block page-css %}
  
  {% endblock %}
</head>
<body>

<h1>這是母板的標題</h1>

{% block page-main %}

{% endblock %}


<h1>母板底部內容</h1> {% block page-js %} {% endblock %} </body> </html>

在子頁面中在頁面最上方使用下面的語法來繼承母板。

{% extends layouts.html‘ %}

然後就可以在子頁面來寫不一樣的內容了。
{% block page-main %}

{% endblock %}


繼承模板的註意事項:
1.模板中可以定義多個block塊,在css樣式和js樣式也可以加block塊:
  
{%block page-css%}

{%endbolck%}



{%block page-js%}
  <script>  
    for(val i = 1; i<5;i++){
      alter(123);
      }
  </script> {
%endbolck%}

2. {%extends ‘base.html’%}要放在第一行

3.{%extends ‘base.html’%} 中要寫字符串,是你要繼承的模板的名字


組件:

有的時候我們只需要模板中的一部分,這時候就用到組件了。

比如說,我們只需要導航欄,我們就單獨把導航欄放在一個html裏作為組件,然後再子頁面中寫

{%inclde ‘ .html’%} 比如:{% include navbar.html‘ %}

靜態文件相關:

  在settings的靜態文件中,STATIC_URL = ‘/static/‘   是這樣保存的,但是如果這個一旦被改動,整個頁面都不能用了,所以我們進行相關改動。

   

{% load static %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />

 這樣就不怕被改動了。或者可以這樣寫:

使用get_static_prefix

{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!" />

或者

{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<img src="{{ STATIC_PREFIX }}images/hi.jpg" alt="Hi!" />
<img src="{{ STATIC_PREFIX }}images/hi2.jpg" alt="Hello!" />

  技術分享圖片                      




django框架之模板系統2