代码布局(自定义的代码,放在哪里) 二种方式:
1. 某个app特有的
-app 目录下,templatetags 文件夹 ** 必需是这个名称的包(目录中有__init__.py文件)
-再到 templatetags 文件夹下创建python模块(py文件)
2. 定义复用
-创建一个新的app,将他们定义在新的app中,在INSTALL_APPS
注册,然后就可以应用
-在 templatetags 文件夹下新建 tags.py #文件名自己取
以下为文件内容:
# 自定义模板标签 和 过滤器 from django.template import Library def to_male(value, arg='zh'): dic = { 'zh': ('女', '男'), 'en': ('famle', 'male') } return dic[arg][value] register = Library() # 实例化 Library对象, *****变量名 必须为:register register.filter('to_male', to_male)
{% load static %} # 加载 静态文件路径设置 {% load tags %} # 加载 自定义<过滤器和 模板标签>库 <div> <table class="table"> <tr> <td>序号</td> <td>名字</td> <td>性别</td> <td>年龄</td> </tr> {#for 模板 endfor#} {% for stu in student %} {# if 模板 endif #} <tr {% if stu.sex == '0' %}style="color:red" {% endif %}> {# url模板 传url 和参数#} <td ><a href="{% url 'student:detail' stu.id %}">{{ stu.id }}</a></td> <td>{{ stu.name}}</td> <td>{{ stu.sex|to_male }}</td> <td>{{ stu.age }}</td> </tr> {% endfor %} </table> </div>
运行!
开门红 : 报错 自定义过滤器没有注册
解决方案:
settings.py templates:中增加:
定义一个包含标签inclusion_tag
# 定义一个包含标签inclusion_tag def show_list_as_ul(value): # value 接收来自 context的变量 ,前提:注册时 takes_context=True return {'ls': value} # 将 value 转换成一个ls, 变量传递给 html模板 register.inclusion_tag(name='show_list_as_ul', filename='../templates/show_list_as_ul.html', takes_context=True) # 接收来自 view 的context变量,用 show_list_as_ul.html模板样式去显示
<!--这 show_as_list 的 inclusion_tag 的模板--> <ul> {% for l in ls %} <li>{{ l }}</li> {% endfor %} #ls 变量 来自模板标签 </ul>
使用时在html模板文件上方 {% load filters %}
# 接收来自 views 的 定义方式
def show_list_as_ul(value): # value 接收来自 context的变量 return {'ls': value} # 将 value 转换成一个ls, 变量传递给 模板 register.inclusion_tag(name='show_list_as_ul', filename='../templates/show_list_as_ul.html', takes_context=True) # 接收来自 view 的context变量,用 show_list_as_ul.html模板样式去显示