Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django中的引导样式_Python_Css_Django_Twitter Bootstrap - Fatal编程技术网

Python Django中的引导样式

Python Django中的引导样式,python,css,django,twitter-bootstrap,Python,Css,Django,Twitter Bootstrap,主要是在bootstrap中看到CSS/HTML,我们遇到了以下模板 <div class="row"> <div class="col-sm-6"> # Add image/ or data you want </div> <div class="col-sm-6"> # Add image/ or data you want </div>

主要是在bootstrap中看到CSS/HTML,我们遇到了以下模板

    <div class="row">
      <div class="col-sm-6">
        # Add image/ or data you want
      </div>
      <div class="col-sm-6">
        # Add image/ or data you want
      </div>
    </div>

    # Second row

    <div class="row">
      <div class="col-sm-6">
        # Add image/ or data you want
      </div>
      <div class="col-sm-6">
        # Add image/ or data you want
      </div>
    </div>

#添加所需的图像/或数据
#添加所需的图像/或数据
#第二排
#添加所需的图像/或数据
#添加所需的图像/或数据
我们如何在Django模板中实现这种行为?我的意思是,当我们在一个值列表上迭代时,我们如何跟踪我们需要开始一个新的
.row

脏溶液 检查循环迭代计数并执行整数操作,以检查是否应该启动新的
。行
元素

想法

这是有效的:

{% for obj in objects %}
{% if not forloop.counter|divisibleby:2 and forloop.last %}
    <div class="row">
        <div class="col-md-6">
            {{ obj }}
        </div>
    </div>
{% elif not forloop.counter|divisibleby:2 %}
    <div class="row">
        <div class="col-md-6">
            {{ obj }}
        </div>
{% elif forloop.counter|divisibleby:2 %}
        <div class="col-md-6">
            {{ obj }}
        </div>
    </div>
{% endif %}
{% endfor %}
{%用于对象%中的obj}
{%如果不是forloop.counter |可除数by:2和forloop.last%}
{{obj}}
{%elif not forloop.counter |可除数b:2%}
{{obj}}
{%elif forloop.counter |可除数b:2%}
{{obj}}
{%endif%}
{%endfor%}
此解决方案使用Django内置的标记和过滤器。您可以考虑创建自定义标记,但这超出了问题的范围。 Django:,

创建一个模板过滤器,用于将列表拆分为块: 用法: 用法:
{%用于图像中的块|块:2%}
{区块中图像的百分比| ljust_列表:2%}
#添加所需的图像/或数据
{%endfor%}
{%endfor%}

如果你想用聪明的方式来解决这个问题,就把问题留给CSS解决。将内部容器内联,浏览器将在一行中容纳尽可能多的图像。如果您不喜欢,请在呈现模板之前在Python代码中将对象列表拆分成块。@Jack您最终使用了什么解决方案?如果找到@Arnar Yngvason,则回答更简单、更全面,并且完成了任务。如果您觉得该问题有帮助,请+1该问题有点复杂,但+1简单,简单,就能完成任务。一个问题是,如何获得相等块的列表?例如,[1,2,3,4,5,6,7,8,9,10]=>[1,2,3],[4,5,6],[7,8,9],[10,无,无]]
from django import template

register = template.Library()

@register.filter
def chunks(l, n):
    n = max(1, int(n))
    return (l[i:i+n] for i in xrange(0, len(l), n))
{% for chunk in images|chunks:2 %}
    <div class="row">
        {% for image in chunk %}
            <div class="col-sm-6">
                # Add image/ or data you want
            </div>
        {% endfor %}
    </div>
{% endfor %}
@register.filter
def ljust_list(l, n):
    """
    ljust_list([1, 2], 4) -> [1, 2, None, None]
    """
    return l + [None] * (n - len(l))

@register.filter
def rjust_list(l, n):
    """
    rjust_list([1, 2], 4) -> [None, None, 1, 2]
    """
    return [None] * (n - len(l)) + l
{% for chunk in images|chunks:2 %}
    <div class="row">
        {% for image in chunk|ljust_list:2 %}
            <div class="col-sm-6">
                # Add image/ or data you want
            </div>
        {% endfor %}
    </div>
{% endfor %}