尝试解析django中模板中的字典时出现模板语法错误

时间:2014-04-06 10:44:10

标签: django django-templates

好的,这是我的观点代码。

def customers_to_be_called(request):
    customers = Customer.objects.filter(call=True)
    list_of_customers = []
    for cust in customers:
        jobs = Job.objects.filter(customer = cust)
        customer_date = {}
        customer_data['customer'] = cust
        customer_date['jobs'] = jobs
        list_of_customers.append(customer_data)

    return render(request, 'repairs/customers_to_be_called.html',
           {'list_of_customers' : list_of_customers,
    })

这是要呈现的模板

<div>
{% for customer in list_of_customers %}
    <h2> {{customer['customer'].name}} </h2>
    <ul>
    {% for job in customer['jobs'] %}
        <li> {{job.product}} </li>
    {% endfor %}
    </ul>
{% endfor %}
</div>

但是当我向此页面发送请求时,我收到以下错误。 Error

我不知道为什么它没有在模板中解析客户,而其中有数据.. ??

1 个答案:

答案 0 :(得分:2)

要引用dict项目,请customer.jobs而非customer['jobs']

所以你的for循环变成了

{% for customer in list_of_customers %}
    <h2> {{customer.customer.name}} </h2>
    <ul>
    {% for job in customer.jobs %}
        <li> {{job.product}} </li>
    {% endfor %}
    </ul>
{% endfor %}