五、自定义过滤器及标签

1 关于自定义

  • 自定义的引入

 

 

templatestemplatetags
存放模板的目录存放自定义标签及过滤器的目录
 

2 代码布局

  • 某个app特有的

    -app 目录下,templatetags 文件夹 -再到 templatetags 文件夹下创建python模块(py文件)

  • 定义复用

    -创建一个新的app,将他们定义在新的app中,在INSTALL_APPS注册,然后就可以应用

3 自定义模板过滤器

自定义过滤器就是一个带有一个或两个参数的Python 函数:

- (输入的)变量的值 —— 不一定是字符串形式。

- 参数的值 —— 可以有一个初始值,或者完全不要这个参数。

 

  • 注册自定义过滤器:
1 django.template.Library.filter()
  1. Library.filter()方法需要两个参数:

    a. 过滤器的名称(一个字符串对象)

    b. 编译的函数 – 一个Python函数(不要把函数名写成字符串)

  2. 可以把register.filter()用作装饰器;

  3. 没有声明 name 参数,Django将使用函数名作为过滤器的名字。

  • 例子

    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. 效果图

4 自定义标签

分为两类:

- 简单标签 django.template.Library.simple_tag()

- 包含标签 django.template.Library.inclusion_tag()

tag()方法有两个参数:

  1. 模板标记的名称 - 字符串。 如果省略,将使用编译函数的名称。

  2. 编译的函数 – 一个Python函数(不要把函数名写成字符串)

与过滤器注册一样,也可以将其用作装饰器。

4.1 简单标签

  • 例子

    1. 在customer_tags.py文件中定义标签的功能,注册标签时使用takes_context 参数,则可以使用从上下文中传入的参数.

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>

4.2 包含标签

  • 例子

    1. 在customer_tags.py文件中定义标签的功能

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. 效果图

5 总结

转载于:https://www.cnblogs.com/weixiansheng/p/10709638.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值