2012-01-12 12 views
0

각 사용자가 템플릿의 표에 표시 할 총 금액을 표시하려고합니다. 이제 콘솔에서 문장을 인쇄 할 때 정확한 값을 얻었지만 템플릿에 {{ total_dollar_amount }}을 입력하면 마지막 값만 표시됩니다.템플릿에 정확한 계산 값이 표시되지 않습니다.

이제는 total_dollar_amount을 반복해야한다고 생각했지만 10 진수 값이 반복 가능하지 않다는 오류가 발생합니다.

누구나 내가 누락 된 부분을 알고 있습니까?

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)) 

답변

1

context 변수는 사전이고; 각 키는 하나의 값만 가질 수 있습니다. investments을 반복하고 각각의 반복에서 동일한 두 개의 키, context['total_dollar_amt']context['counted']을 설정합니다. 따라서 반복 할 때마다 이전 값을 덮어 씁니다. 템플릿 지금

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) 
      # attach value to the investment 
      i.total_dollar_amt = total_amt 
    # attach value to the investment 
    i.counted = count 

: 각 투자에 대한 countedtotal_dollar_amt 값을 반복 할 수있게하려면

, 당신은 context의 키를 설정하지, 투자 목적이를 연결해야 investments을 통해 반복 할 수 있습니다.

1

context['total_dollar_amt']는 할당이 루프에 당한다 때마다 덮어지고 있습니다. 템플릿에 전달할 값을 확인하려면 render_to_response 바로 앞에 print context['total_dollar_amt']을 입력하십시오.

귀하의 설명에서 완전히 명확하지는 않지만 대신 context['investments_data'] = [], 그 다음 루프 context['investments_data'].append({'inv': i, 'total_dollar_amt': total_amt}) 또는 그와 비슷한 문맥 목록을 전달해야한다고 생각합니다. 이어서 템플릿 :

{% for inv_data in investments_data %} 
    {{ inv_data.inv.name }} total: {{ inv_data.total_amt }} 
{% endfor %} 
관련 문제