django模板来填充自举行和列 [英] django template to populate bootstrap rows and columns

查看:164
本文介绍了django模板来填充自举行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是我的问题:我有一堆课程的实例。我想要有一个这些实例对象的表,所以每行最多有六个。在引导术语中,我希望每个对象由类span2的div中的缩略图表示。

So here's my problem: I've got a bunch of instances of a class. I would like to have a sort of table of these instance objects, so that there is a maximum of six in every row. In bootstrap terms, I would like each object to be represented by a thumbnail in a "div" of class "span2".

我最初的冲动是使用嵌套for循环,但是我无法在模板中操作我的索引变量,我无法弄清楚如何做在我的模板之外。

My initial impulse was to use a nested for loop, but I am having trouble manipulating my index variable in the template, and I can't figure out how to do so outside of my template.

这里通常是python / django模板/伪代码,我正在试图找出。

Here is generally what the python/django template/pseudo code is I'm trying to figure out.

queryset = Class.objects.all()
set_length = queryset.count()

num_rows = set_length/6 
#because I want 6 columns in each row, each with one instance

set_as_list = list(queryset) 
# have a list so I can iterate through objects by index

for i in range(table_rows):
    # make a row
    <div class="row">
    for j in range (i*6,(i+1)*6):
        #make six or less columns
        <div class="span2">
           <p>set_as_list[j].attribute1</p>
           <p>set_as_list[j].attribute2</p>
        </div>
    </div> # end row

我希望这种公然混合的django模板语言,python和html不会冒犯有人太糟糕了只是想表达我想要做什么的想法。我会感谢有人可能愿意提供的帮助,因为我一直在努力这几天,并在模板和外部做了相当多的搜索解决方案。

I hope this flagrant mixing of django templating language, python, and html doesn't offend anybody too badly. just trying to express the idea of what I'm trying to do. I would appreciate any help someone may be willing to offer because I've been struggling with this for days and have done quite a bit of searching for a solution both within a template and outside.

我也意识到在整数除法之后,需要有一个最后一行与剩余的对象。

I also realise that there will be need to be a final row with the remainder of objects after the integer division.

推荐答案

没有时间解释,但我有类似的问题,直到我关闭这个浏览器页面是一个解决方案

Have no time to explain, but I've had similar problem and until i closed this browser page here is a solution

{% for sub_article in articles %}
    {% if forloop.first %}<div class="row">{% endif %}
    <div class="col-xs-4">
            <a href="#">
                {{ sub_article.name }}
            </a>
        </div>
    {% if forloop.counter|divisibleby:3 %}</div><div class="row">{% endif %}
    {% if forloop.last %}</div>{% endif %}
{% endfor %}

这篇关于django模板来填充自举行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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