我想在我的模板中显示小计总和。
{% for quote in quotes %}
{% for product in quote.purchase_quote_products_set.all %}
{{product.subtotal}} |
{% endfor %}
<span id="total"></span>
{% endfor %}
我的结果。
15 | 120 | 2000 |
有什么方法可以在 span#total
中显示小计的总和
<span id="total">{{ sum_of_subtotal }}</span>
最佳答案
最好在 Django View 而不是模板中执行此类算法。例如您可以在 View 本身中找到总和:
from django.db.models import Sum
total_price = Quotes.objects.all().annotate(total=Sum('purchase_quote_products__subtotal'))
那么模板可以使用:
<span id="total">{{ quote.total }}</span>
关于python - Django 循环模板中行的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404051/