I have a list of dictionaries with same key information. How can I pass this list of dictionaries to a Django template ?
JavaScript
x
2
1
listdict = [{'product':'specs','price':'12'}, {'product':'shoes','price':'30'}]
2
When trying to send this list via views file to template it fails with an error indicating that only dictionaries are allowed.
Here is the code from views file
JavaScript
1
2
1
return render(request, 'recordings/extensionrecording.html',listDict)
2
Here is the html block-
JavaScript
1
12
12
1
<tbody>
2
{%for list in listDict%}
3
{%for elements in list%}
4
5
6
7
<tr>
8
9
<td class="body-item fonts-style display-4">{{elements.product}}</td>
10
<td class="body-item fonts-style display-4">{{elements.price}}</td>
11
</tr>
12
Advertisement
Answer
Just pass these as an entry of the dictionary:
JavaScript
1
1
1
return render(request, 'recordings/extensionrecording.html',{'listDict': listDict})
Then in the template, you can render these with:
JavaScript
1
4
1
{%for item in listDict %}
2
{{ item.product }}
3
{{ item.price }}
4
{% endfor %}