first I zip all my lists just like
JavaScript
x
2
1
books= zip(ID, bookName, *detail)
2
but as you see the detail has not only one element, I can not sure the number of list of detail. And the number is variable.
Than loop over it in templates like:
JavaScript
1
8
1
{% for ID, bookName, detail, in books %}
2
<tr>
3
<td>{{ ID}}</td>
4
<td>{{ bookName }}</td>
5
<td>{{ detail }} </td>
6
</tr>
7
{% endfor %}
8
As I can not confirm the number of detail, I got the error message like “Need 3 values to unpack in for loop; got 5. “
Is there any method to get the right loop times when the list is variable.
Advertisement
Answer
You can enumerate over the tuples, and then iterate over the elements, so:
JavaScript
1
7
1
{% for record in books %}
2
<tr>
3
{% for item in record %}
4
<td>{{ item }}</td>
5
{% endfor %}
6
</tr>
7
{% endfor %}