I am trying to display the total amount that each user would get in a table on my template. Now when I do print statements in my console, I get the correct values but then when I put {{ total_dollar_amount }} on my template, it only shows me the last value.
Now I thought that I should loop through total_dollar_amount but then it throws an error saying decimal value isn’t iterable.
Anyone know what I am missing?
views.py
def ABD_report(request, *args, **kwargs):
"""
This report will show all 'In Trust For' investments in the system and display all relevant information
"""
from investments.models import Investment
from reports.forms import InTrustForm
context = {}
if request.POST:
form = InTrustForm(request.agents, request.POST)
if form.is_valid():
agents = form.cleaned_data['agents']
context['selected_agents'] = agents
investments = Investment.objects.filter(plan__profile__agent__in=agents, plan__ownership_type__code = "itf")
for i in investments:
#count all members in each plan
count = i.plan.planmember_set.all().count()
#take off the primary member of the account
count -= 1
if i.interestoption:
if i.interestoption.short_label == 'AN':
pay_amt = i.pay_amount
total_amt = (pay_amt / count)
context['total_dollar_amt'] = total_amt
context['counted'] = count
context['investments'] = investments
context['show_report'] = True
else:
form = InTrustForm(request.agents)
context['form'] = form
return render_to_response('reports/admin/abd_report.html', RequestContext(request, context))
The
contextvariable is a dictionary; each key can only have one value. You’re looping throughinvestmentsand setting the same two keys,context['total_dollar_amt']andcontext['counted'], on each loop – so on each iteration you overwrite the previous value.If you want to be able to loop through the
countedandtotal_dollar_amtvalues for each investment, you need to attach this to the investment object, not set a key incontext:Now in your template, you can loop through
investments.