【问题标题】:Template not showing correct calculated values模板未显示正确的计算值
【发布时间】:2012-01-12 20:06:14
【问题描述】:

我正在尝试在我的模板上的表格中显示每个用户将获得的总金额。现在,当我在控制台中打印语句时,我得到了正确的值,但是当我将 {{ total_dollar_amount }} 放在我的模板上时,它只显示最后一个值。

现在我认为我应该循环遍历total_dollar_amount,但它会抛出一个错误,提示十进制值不可迭代。

有人知道我错过了什么吗?

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

【问题讨论】:

    标签: python html django django-templates django-views


    【解决方案1】:

    context 变量是一个字典;每个键只能有一个值。您正在循环遍历 investments 并在每个循环中设置相同的两个键 context['total_dollar_amt']context['counted'] - 所以在每次迭代时您都会覆盖之前的值。

    如果您希望能够循环遍历每个投资的 countedtotal_dollar_amt 值,则需要将其附加到投资对象,而不是在 context 中设置键:

    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
    

    现在在您的模板中,您可以循环访问investments

    【讨论】:

      【解决方案2】:

      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 %}
      

      【讨论】:

        猜你喜欢
        • 2018-07-18
        • 1970-01-01
        • 2014-07-04
        • 1970-01-01
        • 2021-05-21
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        相关资源
        最近更新 更多