I've a list of dictionary.
mylist = [{'id': 1, 'name': 'abc'}, {'id': 2, 'name': 'xyz'}]
I'm passing this mylist
to an html page.
return render(request, "viewdb.html", {'mylist':mylist})
and in viewdb.html
, code is as given below.
{% if mylist %}
<table>
<tr>
<th> ID </th>
<th> Name </th>
</tr>
{% for user in mylist %}
{% for key, value in user.items %}
<tr>
<td> {{ value }} </td>
<td> {{ value }} </td>
</tr>
</table>
{% endfor %}
{% endfor %}
{% endif %}
I want the table to look like this.
ID NAME
1 abc
2 xyz
You don't want to loop over your individual dict values, because you'll get them in more or less random order. You also don't want to create a new <tr>
for each value in the dict. Just do:
{% for user in mylist %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
</tr>
{% endfor %}