Multiply in django template

后端 未结 4 1732
无人及你
无人及你 2020-12-10 14:30

I am looping through cart-items, and want to multiply quantity with unit-price like this:

{% for cart_item in cart.cartitem_set.all %}
{{cart_item.quantity}}         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-10 15:05

    You can do it in template with filters.

    https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters

    From documentation:

    Here’s an example filter definition:

    def cut(value, arg):
        """Removes all values of arg from the given string"""
        return value.replace(arg, '')
    

    And here’s an example of how that filter would be used:

    {{ somevariable|cut:"0" }}
    

提交回复
热议问题