本文介绍了将一个查询器渲染到2个div列(django模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用960的方法将一个查询器枚举成两个 div 列的好方法网格,我有一些事情...

 < div class =container_16> 
< div class =grid_8 alpha>< / div>
< div class =grid_8 omega>< / div>
< / div>

在Django中,一个模型需要在这两列中呈现枚举内容,一样。目前,我有一些丑陋的代码,在视图中将QuerySet分成两半,然后每一半都显示在各自的列中。



有必要做一个更好的方法,最好只使用模板渲染系统?



仅供参考,以下是工作原理那一刻:



views.py



  @render_to('template .html'
def main_athletics_page(request,* args,** kwargs):
sports = Sport.objects.all()
half = sports.count()/ 2
return {'sports_1':sports [0:half],'sports_2':sports [half:]}



template.html



 < div class =grid_8 alpha> 
{%for sports in sports_1%}
<! - Blah blah - >
{%endfor%}
< / div>

< div class =grid_8 omega>
{%for sports_2%}
<! - Blah blah - >
{%endfor%}
< / div>


解决方案

我建议使用。



Django片段提供,您可以使用以下内容:

  {%load listutil%} 

< div class =grid_8 alpha>
{%体育运动|分区:2|第一%}
<! - Blah Blah - >
{%endfor%}
< / div>

< div class =grid_8 omega>
{%体育运动|分区:2| last%}
<! - Blah Blah - >
{%endfor%}
< / div>


Is there a good way to render the enumeration of a queryset into two div columns?

Using 960 grid, I've got something to the effect of...

<div class="container_16">
    <div class="grid_8 alpha"></div>
    <div class="grid_8 omega"></div>
</div>

In Django, one model needs to have it's enumerated contents rendered in both of those columns, and preferably somewhat equally. For the moment, I've got some ugly code that in the view splits the QuerySet into 2 halves, and then each half is rendered in their respective column.

There's got to be a better way to do this, preferably using only the template rendering system?

Just for reference, here's how it "works" at the moment:

views.py

@render_to('template.html')
def main_athletics_page(request, *args, **kwargs):
    sports = Sport.objects.all()
    half = sports.count() / 2
    return { 'sports_1' : sports[0:half], 'sports_2' : sports[half:] }

template.html

<div class="grid_8 alpha">
    {% for sport in sports_1 %}
        <!-- Blah blah -->
    {% endfor %}
</div>

<div class="grid_8 omega">
    {% for sport in sports_2 %}
        <!-- Blah blah -->
    {% endfor %}
</div>
解决方案

I recommend using Django filters.

Django snippets provides a partitioning template filter, which you can use like:

{% load listutil %}

<div class="grid_8 alpha">
    {% for sport in sports|partition:"2"|first %}
        <!-- Blah Blah -->
    {% endfor %}
</div>

<div class="grid_8 omega">
    {% for sport in sports|partition:"2"|last %}
        <!-- Blah Blah -->
    {% endfor %}
</div>

这篇关于将一个查询器渲染到2个div列(django模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:37