Django模板:Threes中的组项目 [英] Django templates: Group items in Threes

查看:117
本文介绍了Django模板:Threes中的组项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题Django。我有一组对象,它在模板中循环,正如你通常。但是,我需要将项目分组三。页面布局如下:



绘画1 - 绘画2 - 绘画3



说明1 < br>
DESCRIPTION 2

DESCRIPTION 3



绘画4 - 绘画5绘画6



描述4

描述5

描述6



等etc

我无法找出最好的Django标签来做到这一点。它似乎有点棘手。



除非当然,我做一些java脚本黑客攻击,让Django脱离这个?必须有某种说法把所有的描述divs一个接一个或类似。不知道如何最好地玩这个顺序。有什么想法吗?干杯。

解决方案

每当你发现自己在模板中尝试复杂的代码,它通常是一个很好的指示,已经提出了一个替代解决方案,它是将代码移动到你的视图函数。



另一个解决方案是通过一个新的模板标签。选择此解决方案的原因之一是,您可以轻松地重复使用提供不同视图的网页的代码。

  class GroupPaintingsNode(template.Node):
def __init __(self,num,varname):
self.num,self.varname = int var b

def render(self,context):
paintings = Painting.objects.all#做你的获取/过滤这里..
l = [[] for i in range len(paintings))]
i = 0
while i < len(painting):
l [i] .append([p.title for p in paintings [i:i + self.num]])
l [i] .append([p.desc for p in painting [i:i + self.num]])
i + = self.num
context [self.varname] = l
return''

def group_paintings(parser,token):
tokens = token.contents.split()
如果len(tokens)!= 4:
raise template.TemplateSyntaxError,'%s'参数%tokens [0]
if tokens [2]!='as':
raise template.TemplateSyntaxError,%s标记的第二个参数必须为%tokens [0 ]
return groupPaintingsNode(tokens [1],tokens [3])
group_paintings = register.tag(group_paintings)

在模板代码中,你可以这样使用它:

  {%group_paintings 3 as paintings% } 
{%for p in paintings%}
{%for title in p.0%} {{title}} {%endfor%}< br>
{%for desc in p.1%} {{desc}} {%endfor%}
{%endfor%}


I have an odd problem with Django. I have a set of objects that Im looping through in a template as you would normally. However, I need to group the items in threes. The layout of the page goes like this:

Painting 1 - Painting 2 - Painting 3

D E S C R I P T I O N 1
D E S C R I P T I O N 2
D E S C R I P T I O N 3

Painting 4 - Painting - 5 Painting 6

D E S C R I P T I O N 4
D E S C R I P T I O N 5
D E S C R I P T I O N 6

etc etc

I cant quite figure out the best set of Django tags to do this really. It seems somewhat tricky. the {% cycle %} statement didnt quite help.

Unless of course, I do some java script hacking of some kind and leave Django out of this? There must be someway of saying "Put all the description divs after one another" or similar. Not sure how best to play with this ordering. Any thoughts? Cheers.

解决方案

Whenever you find yourself trying out complex code inside templates, its usually a good indication that it should be moved elsewhere. One of the alternative solutions have already been suggested, which is to move the code into your view function.

The other solution would be to expose the functionality via a new template tag. One of the reasons you would choose this solution over the view solution, is that you'll be able to easily re-use the code for pages that are served with different views.

class GroupPaintingsNode(template.Node):
    def __init__(self, num, varname):
        self.num, self.varname = int(num), varname

    def render(self, context):
        paintings = Painting.objects.all # do your fetching/filtering here.. 
        l = [[] for i in range(len(paintings))]
        i = 0
        while i < len(paintings):
            l[i].append([p.title for p in paintings[i:i+self.num]])
            l[i].append([p.desc for p in paintings[i:i+self.num]])
            i += self.num
        context[self.varname] = l
        return ''

def group_paintings(parser, token):
    tokens = token.contents.split()
    if len(tokens) != 4:
        raise template.TemplateSyntaxError, "'%s' tag requires three arguments" % tokens[0]
    if tokens[2] != 'as':
        raise template.TemplateSyntaxError, "Second argument to '%s' tag must be 'as'" % tokens[0]
    return GroupPaintingsNode(tokens[1], tokens[3])
group_paintings = register.tag(group_paintings)

In template code you would use it like this:

{% group_paintings 3 as paintings %}
{% for p in paintings %}
    {% for title in p.0 %} {{ title }} {% endfor %}<br>
    {% for desc in p.1 %} {{ desc }} {% endfor %}
{% endfor %}

这篇关于Django模板:Threes中的组项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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