您需要使用自定义模板标签。模板过滤器仅接受一个参数,而自定义模板标签可以接受所需数量的参数,进行乘法运算并将值返回到上下文。
您将要查看Django模板标签文档,但是一个简单的示例是:
from django import templateregister = template.Library()@register.simple_tag()def multiply(qty, unit_price, *args, **kwargs): # you would need to do any localization of the result here return qty * unit_price
您可以这样称呼:
{% load your_custom_template_tags %}{% for cart_item in cart.cartitem_set.all %} {% multiply cart_item.quantity cart_item.unit_price %}{% endfor %}您确定不想使此结果成为购物车项目的属性吗?结帐时,您似乎需要将此信息作为购物车的一部分。



