简化:@register.simple_tag
def current_time(token):
return datetime.datetime.now().strftime(str(token))
templatetags目录:
my_tag_filter.py:
'''自定义标签与过滤器''' from django import template register = template.Library() @register.simple_tag def multi_filter(a,b)->str: '''a 是整形 b是 float''' if a=='' or b== '': return '0' return '{:.2f}'.format(int(a)*float(b)) # #Decimal乘法 # @register.simple_tag # def Decimal_multiplication(a,b)->str: # from decimal import Decimal # x=Decimal(a)*Decimal(b) # return str(x) @register.simple_tag def get_int(a)->int: ''' :param a: 数量 :return: 整形数量 ''' if a == '': a=0 return int(a) # 用户中心---订单总价 @register.simple_tag def get_orderAll(a)->str: ''' :param a: 订单list :return: 订单总价 ''' num = 0 for i in a: num += float(i.order_detail_price) * int(i.order_detail_num) return '{:.2f}'.format(num)
应用:
{% load my_tag_filter %} <td width="15%">订单总价{% get_orderAll n %}元</td> {% load my_tag_filter %} <li class="col04">{% multi_filter x.order_detail_num x.order_detail_price %} 元</li>