Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to for-loop three columns per row in Django/python?

I would like to display data, three columns per row during my for. I would like my result to look like the following:

<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>

Anyone know how to do it?

Syntax Error TemplateSyntaxError at /

'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)

like image 301
Dusean Singh Avatar asked Dec 06 '10 21:12

Dusean Singh


1 Answers

There's a divisibleby tag.

So you can do something (ugly) like:

<table><tr>
{% for field in form %}
   <td>{{ field }}</td>
   {% if forloop.last %}
     </tr>
   {% else %}
     {% if forloop.counter|divisibleby:"3" %}
       </tr><tr>
     {% endif %}
   {% endif %}
{% endfor %}
</table>

Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).

like image 122
dr jimbob Avatar answered Nov 02 '22 05:11

dr jimbob