Getting template syntax error while trying to parse a dictionary in my template in django

StackOverflow https://stackoverflow.com/questions/22892932

  •  28-06-2023
  •  | 
  •  

Question

Ok here is the code of my view.

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

and here is the template that is gonna be rendered

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

But When I send a request to this page I get the following error. Error

I don't know why it isn't parsing customer in the template while there is data in it..??

Was it helpful?

Solution

To refer to dict item do customer.jobs not customer['jobs']

So your for loop becomes

{% for customer in list_of_customers %}
    <h2> {{customer.customer.name}} </h2>
    <ul>
    {% for job in customer.jobs %}
        <li> {{job.product}} </li>
    {% endfor %}
    </ul>
{% endfor %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top