使用Django将数据分解成多个显示列 [英] Breaking data into multiple display colums with Django
问题描述
我正在寻找一个最佳方法来实现我的QuerySet的多列显示的建议它将每列从顶部到顶部填充X列。这意味着每列中的项目数量等于QuerySet计数除以X(列数)。
使用Offset不适用于此,因为我希望我的数据增长到4列,而不用手动更新偏移量。 CSS浮动在视觉上工作,但使数据无序。
这样的东西应该适合你,将列数传递给列
到模板:
{%for items in items%}
{%if forloop.first% }< div style =float:left;> {%endif%}
{{item}}
{%ifloop.counter | divisibleby:columns%}
< / div>< div style =float:left>
{%endif%}
{%if forloop.last%}< / div> {%endif%}
{%endfor%}
< div style =明确:两者;>< / DIV>
Overlap in terminology makes search for answers difficult for this one.
I'm looking for advice on the best way to implement a multiple-column display of my QuerySet that fills each column top to bottom over X columns. Meaning that the number of items in each column equals the QuerySet count divided by X (number of columns).
Using Offset doesn't work for this because I would like my data to grow into 4 columns without updating the offset manually. CSS floats work visually, but leave the data out of order.
Something like that should work for you, pass the number of columns as columns
to the template:
{% for item in items %}
{% if forloop.first %}<div style="float:left;">{% endif %}
{{ item }}
{% if forloop.counter|divisibleby:columns %}
</div><div style="float:left">
{% endif %}
{% if forloop.last %}</div>{% endif %}
{% endfor %}
<div style="clear:both;"></div>
这篇关于使用Django将数据分解成多个显示列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!