|
-
定义复用
-创建一个新的app,将他们定义在新的app中,在INSTALL_APPS注册,然后就可以应用
自定义过滤器就是一个带有一个或两个参数的Python 函数:
- (输入的)变量的值 —— 不一定是字符串形式。
- 参数的值 —— 可以有一个初始值,或者完全不要这个参数。
- 注册自定义过滤器:
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>序号</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. 效果图
分为两类:
- 简单标签 django.template.Library.simple_tag()
- 包含标签 django.template.Library.inclusion_tag()
tag()方法有两个参数:
-
模板标记的名称 - 字符串。 如果省略,将使用编译函数的名称。
-
编译的函数 – 一个Python函数(不要把函数名写成字符串)
与过滤器注册一样,也可以将其用作装饰器。
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文件中使用标签
1 {% load static %} 2 {% load customer_tags %} 3 4 <h1>当前时间:{% 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文件中使用包含标签
1 {% load static %} 2 {% load customer_tags %} 3 4 <table class="table"> 5 <thead> 6 <tr> 7 <th>序号</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. 视图函数views.py
1 def new_index(request): 2 student_table = [ 3 {'name': '张三', 'gender': 1, 'age': 21, 'id': 56, 'course': ['渗流力学', '油层物理', "油藏工程"]}, 4 {'name': '李四', 'gender': 0, 'age': 18, 'id': 49, 'course': ['高等渗流力学', '油气田开发', "高等油藏工程"]}, 5 {'name': '王五', 'gender': 1, 'age': 25, 'id': 106, 'course': ['数值分析', '模糊数学', "应用统计"]}, 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. 效果图