Multiply in django template

后端 未结 4 1736
无人及你
无人及你 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 14:47

    Or you can set the property on the model:

    class CartItem(models.Model):
        cart = models.ForeignKey(Cart)
        item = models.ForeignKey(Supplier)
        quantity = models.IntegerField(default=0)
    
        @property
        def total_cost(self):
            return self.quantity * self.item.retail_price
    
        def __unicode__(self):
            return self.item.product_name
    

提交回复
热议问题