Reputation: 3378
I have a function to view report:
def view_report(request):
a = Bill.objects.all()
return render_to_response('report.html', {'a':a}, context_instance=RequestContext(request))
There's a attribute called price which has numeric elements. I want to sum all the prices and show as one number in Django template:
{% for i in a %}
{{ i.price }}
{% endfor %}
This simply returns all the prices. I want to sum all those prices and show as one. I tried using {{ i.price|sum }}
which didn't work.
Upvotes: 2
Views: 7719
Reputation: 599530
Don't do that in the template. Use the aggregation API in your view:
from django.db.models import Sum
total_price = Bill.objects.all().aggregate(Sum('price'))
Upvotes: 9