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

查看:141
本文介绍了使用Django将数据分成多个显示列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

术语的重叠使得对此问题的搜索变得困难。

Overlap in terminology makes search for answers difficult for this one.

我正在寻找实现QuerySet的多列显示的最佳方法的建议在X列的顶部至底部填充每列。这意味着每列中的项目数等于QuerySet计数除以X(列数)。

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

无法使用此功能,因为我希望我的数据增加到4列,而无需手动更新该位置。 CSS可视化地浮动工作,但使数据混乱。

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天全站免登陆