Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a list as a 2-column html table in a django template

I have an ordered list that I'm rendering as a 2-column html table in a Django template. In the table, the elements should be output so that the first (n+1)/2 elements are in the first column and the remainder are in the second column, as follows:

<table>
  <tr>
     <td>Elem 1</td>
     <td>Elem 4</td>
  </tr>
  <tr>
    <td>Elem 2</td>
    <td>Elem 5</td>
  </tr>
  <tr>
    <td>Elem 3</td>
    <td></td>
  </tr>
</table>

Assuming that my elements are in the context as {{ elems }}, how could I reasonably accomplish this in a Django template?

like image 327
B Robster Avatar asked Aug 08 '12 12:08

B Robster


1 Answers

Check this recipe. You might have to modify it, but you will get the idea.

Using it, in your template you would just do:

{% for row in elems|columns:2 %}
 <tr>
    {% for item in row %}
        <td>{{ item }}</td>
    {% endfor %}
 </tr>
{% endfor %}
like image 71
Mikael Avatar answered Nov 15 '22 05:11

Mikael