How to occupy three columns per row in Django / python?

I would like to display data, three columns per row during mine. I would like my result to look like this:

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

Does anyone know how to do this?

Syntax error TemplateSyntaxError at /

Operators

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

+8
python django for-loop
source share
2 answers

There is 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 can specify a table_print form class that returns an html string (wrapped in mark_safe ).

+22
source share
 <table> {% for i in range(0, len(stuff), 3) %} <tr> {% for j in range(3) %} <td>{{ stuff[i+j] }}</td> {% endfor %} </tr> {% endfor %} </table> 

Sorry for the misunderstood question.

-2
source share

Source: https://habr.com/ru/post/649881/


All Articles