I have a list of 16 results, let's call it "results". I want to arrange them in a 4 x 4 table.
Using the django template, how can I do this? (It doesn't seem like cycle would help me here)
<table>
{% for r in results %}
...?
{% endfor %}
</table>
Thanks!!
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.
What is URL Template Tag? The URL template tag is a typical type of Tag in the Django Template Language framework. This tag is specifically used to add View URLs in the Template Files.
You can use the cycle
tag for this.
<table>
{% for r in results %}
{% cycle '<tr>' '' '' '' %}
<td>{{r.content}}</td>
{% cycle '' '' '' '</tr>' %}
{% endfor %}
</table>
Would output something like...
<table>
<tr>
<td>result 1</td>
<td>result 2</td>
<td>result 3</td>
<td>result 4</td>
</tr>
<tr>
<td>result 5</td>
<td>result 6</td>
<td>result 7</td>
<td>result 8</td>
</tr>
<!-- etc -->
</table>
Suppose you have:results=[[1, 2, 3, 4,], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
The following template can be used to display results in a table:
<table>
{% for rowval in results %}
<tr>
{% for val in rowval %}
<td>{{val}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
It will display:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With