I need to make a table in a Django template with variable columns, and I've been trying to do so by using the following code:
Suppose I have headers like :
headers = ['date', 'a', 'b', 'c']
body = [{'date': '2015-10-16', 'a':1, 'b':2, 'c':3},
{'date': '2015-10-17', 'a':4, 'b':5, 'c':6},
....]
As you can see headers correspond to keys of the body values. So I've been trying to do that using for nested loops but, I failed.
<table >
<thead>
<tr>
{% for th in headers %}
<th>{{th}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for bd in body %}
<tr>
{% for h in header %}
{% with h as key %}
<td>{{bd.key}}</td>
{% endwith %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
The {{bd.key}} is not displaying. Any solution ? or I need to rework my table.