用于填充引导行和列的 django 模板 [英] django template to populate bootstrap rows and columns

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

问题描述

所以这是我的问题:我有一堆类的实例.我想要一种这些实例对象的表格,以便每行最多有六个.在引导程序方面,我希望每个对象都由span2"类的div"中的缩略图表示.

我最初的想法是使用嵌套的 for 循环,但是我在模板中操作索引变量时遇到了问题,而且我不知道如何在模板之外执行此操作.

这通常是我试图弄清楚的 python/django 模板/伪代码.

queryset = Class.objects.all()set_length = queryset.count()num_rows = set_length/6#因为我想要每行 6 列,每列有一个实例set_as_list = 列表(查询集)# 有一个列表,这样我就可以通过索引遍历对象对于范围内的 i(table_rows):# 排成一行<div class="row">对于范围内的 j (i*6,(i+1)*6):#制作六列或更少列<div class="span2"><p>set_as_list[j].attribute1</p><p>set_as_list[j].attribute2</p>

# 结束行

我希望 django 模板语言、python 和 html 的这种公然混合不会太冒犯任何人.只是想表达我正在尝试做的事情的想法.如果有人愿意提供任何帮助,我将不胜感激,因为我已经为此苦苦挣扎了好几天,并且已经在模板内和外部搜索了很多解决方案.

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

解决方案

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

{% for sub_article in article %}{% if forloop.first %}

{% endif %}<div class="col-xs-4"><a href="#">{{ sub_article.name }}</a>

{% if forloop.counter|divisibleby:3 %}</div><div class="row">{% endif %}{% if forloop.last %}

{% endif %}{% 结束为 %}

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

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.

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

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