I'm having difficulty wrapping my head around a simple problem. How do you calculate totals and subtotals in a django template?
Let's say I want to generate a report of customer's orders, something like:
Desired Report Output
Customer1
1 Widgets $ 2
1 Bobbins $ 1
Subtotal $ 3
Customer2
2 Widgets $ 4
2 Bobbins $ 2
Subtotal $ 6
TOTAL $ 9
Let's assume we populate a dictionary in our view
orgs = {}
orgs['Customer1'] = [
{ 'qty': 1, 'descr' : 'Widgets', 'price': 2 },
{ 'qty': 1, 'descr' : 'Bobbins', 'price': 1 },
]
...
And a template like this:
{% for org,orders in orgs.items %}
<p>{{ org }}
{% for order in orders %}
<ul>
<li>{{ order.qty }}</li>
<li>{{ order.descr }}</li>
<li>{{ order.price }}</li>
</ul>
...
Any idea on how to calculate totals/subtotals?
I understand the basic recommendation is to do this in the view, but I can't figure out how to put this in the orgs dict. And trying to use a parallel data structure does not seem to be possible according to the django docs (https://docs.djangoproject.com/en/dev/ref/templates/api/#variables-and-lookups).
Any ideas?