?
?
| ? |
-
定義復(fù)用
-創(chuàng)建一個(gè)新的app,將他們定義在新的app中,在INSTALL_APPS注冊,然后就可以應(yīng)用
自定義過濾器就是一個(gè)帶有一個(gè)或兩個(gè)參數(shù)的Python 函數(shù):
- (輸入的)變量的值 —— 不一定是字符串形式。
- 參數(shù)的值 —— 可以有一個(gè)初始值,或者完全不要這個(gè)參數(shù)。
?
- 注冊自定義過濾器:
1 django.template.Library.filter()
- 例子
1.?在customer_filters.py文件中自定義to_male的過濾器功能,并注冊
1 from django.template import Library 2 3 register = Library() 4 5 @register.filter() 6 def to_male(value, arg='zh'): 7 map = { 8 'zh': ('女', '男'), 9 'en': ('female', 'male') 10 } 11 return map[arg][value]
2.?使用自定義過濾器
1 {% load static %} 2 {% load customer_filters %} 3 4 <table class="table"> 5 <thead> 6 <tr> 7 <th>序號(hào)</th> 8 <th>姓名</th> 9 <th>性別</th> 10 <th>年齡</th> 11 <th>課程</th> 12 </tr> 13 </thead> 14 <tbody> 15 {% for i in students %} 16 <tr {% if i.gender == 0 %}style="color: red"{% endif %}> 17 <th>{{ forloop.counter }}</th> 18 <th><a href="{% url 'teacher:detail' i.id %}">{{ i.name }}</a></th> 19 <th>{{ i.gender|to_male:'en' }}</th> 20 <th>{{ i.age }}</th> 21 <th>{% show_list_as_ul i.course style='link' %}</th> 22 </tr> 23 {% endfor %} 24 </tbody> 25 </table>
3. 效果圖
分為兩類:
- 簡單標(biāo)簽 django.template.Library.simple_tag()
- 包含標(biāo)簽 django.template.Library.inclusion_tag()
tag()方法有兩個(gè)參數(shù):
-
模板標(biāo)記的名稱 - 字符串。 如果省略,將使用編譯函數(shù)的名稱。
-
編譯的函數(shù) – 一個(gè)Python函數(shù)(不要把函數(shù)名寫成字符串)
與過濾器注冊一樣,也可以將其用作裝飾器。
1 from datetime import datetime 2 from django.template import Library 3 4 register = Library() 5 6 @register.simple_tag(name='current', takes_context=True) 7 def current_time(context): 8 return datetime.now().strftime(context['format_str'])
2.?在index.html文件中使用標(biāo)簽
1 {% load static %} 2 {% load customer_tags %} 3 4 <h1>當(dāng)前時(shí)間:{% current %}</h1>
1 from django.template import Library 2 3 register = Library() 4 5 @register.inclusion_tag('teacher/show_list_as_ul.html') 6 def show_list_as_ul(value, style): 7 return {'ls': value, 'style': style}
2.?在show_list_as_ul.html文件中存放功能代碼
1 {% if style == 'button' %} 2 <div class="list-group"> 3 {% for l in ls %} 4 <button type="button" class="list-group-item">{{ l }}</button> 5 {% endfor %} 6 </div> 7 {% elif style == 'link' %} 8 <div class="list-group"> 9 {% for l in ls %} 10 <a href="#" class="list-group-item">{{ l }}</a> 11 {% endfor %} 12 </div> 13 {% else %} 14 <ul class="list-group"> 15 {% for l in ls %} 16 <li class="list-group-item">{{ l }}</li> 17 {% endfor %} 18 </ul> 19 {% endif %}
3.?在index.html文件中使用包含標(biāo)簽
1 {% load static %} 2 {% load customer_tags %} 3 4 <table class="table"> 5 <thead> 6 <tr> 7 <th>序號(hào)</th> 8 <th>姓名</th> 9 <th>性別</th> 10 <th>年齡</th> 11 <th>課程</th> 12 </tr> 13 </thead> 14 <tbody> 15 {% for i in students %} 16 <tr {% if i.gender == 0 %}style="color: red"{% endif %}> 17 <th>{{ forloop.counter }}</th> 18 <th><a href="{% url 'teacher:detail' i.id %}">{{ i.name }}</a></th> 19 <th>{{ i.gender|to_male:'en' }}</th> 20 <th>{{ i.age }}</th> 21 <th>{% show_list_as_ul i.course style='link' %}</th> 22 </tr> 23 {% endfor %} 24 </tbody> 25 </table>
4.?視圖函數(shù)views.py
1 def new_index(request): 2 student_table = [ 3 {'name': '張三', 'gender': 1, 'age': 21, 'id': 56, 'course': ['滲流力學(xué)', '油層物理', "油藏工程"]}, 4 {'name': '李四', 'gender': 0, 'age': 18, 'id': 49, 'course': ['高等滲流力學(xué)', '油氣田開發(fā)', "高等油藏工程"]}, 5 {'name': '王五', 'gender': 1, 'age': 25, 'id': 106, 'course': ['數(shù)值分析', '模糊數(shù)學(xué)', "應(yīng)用統(tǒng)計(jì)"]}, 6 {'name': '趙六', 'gender': 0, 'age': 29, 'id': 21, 'course': ['凝析氣藏', '油氣藏流體', "多相流理論"]}, 7 ] 8 format_str = '%Y-%m-%d %H:%M:%S' 9 return render(request, 'teacher/index.html', 10 context={ 11 'students': student_table, 12 'format_str': format_str, 13 } 14 )
5. 效果圖