使用Django将数据分解成多个显示列 [英] Breaking data into multiple display colums with Django

查看:128
本文介绍了使用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屋!

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