Django sum of row in template for loop
django, python
Solution
It is best to perform such arithmetic in Django views rather than templates. For e.g. you can find the sum in the view itself:
from django.db.models import Sum
total_price = Quotes.objects.all().annotate(total=Sum('purchase_quote_products__subtotal'))
Then the template can use:
<span id="total">{{ quote.total }}</span>
Problem
I want to show sum of subtotal in my template. ``` {% for quote in quotes %} {% for product in quote.purchase_quote_products_set.all %} {{product.subtotal}} | {% endfor %} <span id="total"></span> {% endfor %} ``` my result. ``` 15 | 120 | 2000 | ``` Is there any way to show sum of subtotal inside `span#total` ``` <span id="total">{{ sum_of_subtotal }}</span> ```