1. 程式人生 > >Django自定義模板函數

Django自定義模板函數

模板文件 文件 unset rar ise rom www. blog ref

https://www.cnblogs.com/SunsetSunrise/p/7680491.html

在django中新建一個應用:listpage
在listpage中新建templatetags文件夾
在templatetags文件夾中新建XX.py文件
在XX.py中寫入:

from django import template

register = template.Library()


@register.simple_tag()
def YY(a,b):
    return a+b

  

在模板文件中寫入

{% load XX %} <!--一定要寫在第一行開頭-->
...
{% YY 1 2 %} <!--函數會計算:1+2-->
...

  

運行模板,得到3

Django自定義模板函數