i want to display records form dictionary which contains list as value in table format.
my dictionary looks like this and i am passing this dictionary through view.py file as {'data':datas}
enroll_number=[1,2,3]
names = ['tom','mike','john']
total = [80,90,80]
datas={
'id':enroll_number,
'names':names,
'total':avgs,
}
in my HTML page my code is something like this:
<table class="table table-striped">
<tr class="text-center">
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Total</th>
</tr>
{% for key, value_list in datas.items %}
<tr>
{% for value in value_list %}
<td>{{value}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
so i am getting output like this:
Id Name Total
1 2 3
tom mike john
80 90 80
But i want output like this
Id Name Total
1 tom 80
2 mike 90
3 john 80
You will need to zip them in your view and iterate through the joined list.
data = zip(enroll_number, names, avgs)
...
{% for row in data %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
Convert your table data to be rows-oriented instead of columns-oriented:
In [2]: [dict(zip(['id', 'names', 'total'], x)) for x in zip(enroll_number, names, total)]
Out[2]:
[{'id': 1, 'names': 'tom', 'total': 80},
{'id': 2, 'names': 'mike', 'total': 90},
{'id': 3, 'names': 'john', 'total': 80}]
And then iterate over rows in your template.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.