如何在Django / python中每行循环三列? [英] How to for-loop three columns per row in Django/python?

查看:440
本文介绍了如何在Django / python中每行循环三列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示数据,在我的期间每行三列。我希望我的结果如下所示:

 < table> 
将TR>< TD> VALUE1< / TD>< TD> VALUE2< / TD>< TD> VALUE3< / TD>< / TR>
将TR>< TD> VALUE4< / TD><< TD>值5< / TD>< TD> VALUE6< / TD>< / TR>
< / table>

任何人都知道该怎么做?



语法错误
TemplateSyntaxError at /



'for'语句应该使用格式'for x在y'中:对于范围(0,len(all_products_list),3)

解决方案

有一个divisibleby标记。所以你可以做一些丑陋的事情,如:

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

或者,你可以给你的表单类一个 table_print 返回一个html字符串的方法(包装在 mark_safe 中)。


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)

解决方案

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).

这篇关于如何在Django / python中每行循环三列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆