关于 Django 的模板这里有详细介绍 http://www.runoob.com/django/django-template.html
1, 在 app 下创建 templatetags 的目录, 在目录下创建 test.py 文件
2,test.py 内容
- #!/usr/bin/env python
- #created by Baird
- from django import template
- register = template.Library() #注册
- @register.simple_tag()
- def Custom_add(a,b):
- return a+b
- @register.filter()
- def Custom_multiply(a):
- return a*a
3,simple_tag 和 filter 的使用方式 (在 template 中的 html 文件中使用)
- {% load test %} {# 加载 #}
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- {% Custom_add 1 2 %} {# simple_tag #}
- {{ 5|Custom_multiply }} {# filter #}
- </body>
- </html>
来源: http://www.bubuko.com/infodetail-2744414.html