1.自定義的引入
内置函數---------------------->自定義函數
内置子產品---------------------->自定義子產品
内置過濾器------------------->自定義過濾器
内置标簽---------------------->自定義标簽
2.目錄結構
1.在項目目錄下建立一個名為common的python包
2.将common加入到settings檔案中的INSTALLED_APP清單中
3.在common裡面建立目錄templatetags,在目錄裡面建立自定義過濾器及标簽檔案。

settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'book',
'movie',
'common',
]
3.模闆過濾器的分析
{{var|foo:“bar”}}
var:變量
foo:過濾器
bar:參數
自定義過濾器就是一個帶有一個或兩個參數的Python函數:
(輸入的)變量的值----不一定是字元串形式。
參數的值----可以有一個初始值,或者完全不要這個參數
4.注冊自定義過濾器
django.template.Library.filter()
1.Library.filter()方法需要兩個參數:
a.過濾器的名稱(一個字元串對象)
b.編譯的函數,一個python函數(不要把函數名寫成字元串)
2.可以把register.filter()用作裝飾器
3.沒有聲明name函數,Django将使用函數名作為過濾器的名字。
5.自定義過濾器
common_test.py
from django import template
#擷取注冊的執行個體對象,名字是固定的
register = template.Library()
#裝飾器,在函數的基礎上,增加額外的功能
@register.filter
def lowers(value):
return value.lower()
book_index.html
在html最上方加載common_test
定義的函數名為lowers同之前的内置過濾器lower作用一樣。
如果将@register.filter改為@register.filter(‘Name’),在調用時,可以用Name來代替命名的函數名。
6.自定義标簽
簡單标簽 django.template.Library.simple_tag()
包含标簽django.template.Library.inclusion_tag()
tag()方法有兩個參數:
1.模闆标記的名稱 - 字元串。如果省略,将使用編譯函數的名稱。
2.編譯的函數-一個Python函數(不要把函數名稱寫成字元串)與過濾器注冊一樣,也可以将其用作裝飾器。
例1:
#自定義簡單标簽
@register.simple_tag()
def current_time():
format_string = '%Y/%m/%d %H:%M:%S'
return datetime.datetime.now().strftime(format_string)
例2
#自定義簡單标簽-傳參
@register.simple_tag()
def current_time1(format_string):
return datetime.datetime.now().strftime(format_string)
例3
book/views.py
def book_index2(request):
return render(request,'book/book_index.html',context={'name':fru.name,
'age':fru.age,
'list':ls,
'tuple':tu,
'dict':dc,
'str':str,
'fruits':fru,
'fruits1':fru.say,
'hello':hello('aaa'),
'set':se,
'test':None,
'num1':11,
'num2':22,
'now':datetime.datetime.now,
'tag':'<h1>我是标題标簽</h1>',
'float':3.1415926,
'test1':'<:',
'format_string': '%Y/%m/%d %H:%M:%S'
})
common_test.py
#自定義簡單标簽-上下文
@register.simple_tag(takes_context=True)
def current_time2(context):
format_string = context.get('format_string')
return datetime.datetime.now().strftime(format_string)
book_index.html
三種方法都可以實作
{% current_time %}<br>
{% current_time1 '%Y/%m/%d %H:%M:%S' %}<br>
{% current_time2 %}
7.自定義包含标簽
包含标簽的功能是可以通過渲染另外一個模闆來顯示一些資料,将另一頁面的資料顯示在指定的模闆頁面中。
show_tag.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自定義包含标簽</title>
</head>
<body>
{% for foo in counter %}
{{ foo }}
{% endfor %}
</body>
</html>
例1
common_test.py
#自定義包含标簽
@register.inclusion_tag('book/show_tag.html')
def show_result():
li = ['python','java','c++','web']
return {'counter':li}
book_index.html
例2
common_test.py
#自定義包含标簽-傳參
@register.inclusion_tag('book/show_tag.html')
def show_reslut1(value):
return {'counter':value}
book_index.html
例3
common_test.py
#自定義包含标簽-上下文管理器
@register.inclusion_tag('book/show_tag.html',takes_context=True)
def show_reslut2(context):
value = context.get('tuple')
return {'counter':value}
book_index.py