在Django中,如何按年份显示对象列表 [英] In Django how to show a list of objects by year

查看:284
本文介绍了在Django中,如何按年份显示对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些模型:

class Year(models.Model):
    name = models.CharField(max_length=15)
    date = models.DateField()

class Period(models.Model):
    name = models.CharField(max_length=15)
    date = models.DateField()

class Notice(models.Model):
    year = models.ForeignKey(Year)
    period = models.ForeignKey(Period, blank=True, null=True)
    text = models.TextField()
    order = models.PositiveSmallIntegerField(default=1)

在我看来,我想显示年度和期间的所有通知,但是如果与...相同,则不想重复年份和/或通知期限以前。我想要这种结果:

And in my view I would like to show all the Notices ordered by year and period but I don't want to repeat the year and/or the period for a notice if they are the same as the previous. I would like this kind of result:


1932-1940

中年夏天


  • 来自通知1的文字Lorem ipsum ...

  • Text Lorem ipsum from notice 1 ...

来自通知2的文本Lorem ipsum ...

Text Lorem ipsum from notice 2 ...

/ i>


  • 来自通知3的文本Lorem ipsum ...

1950

January


  • 文字Lorem ipsum from notice 4 ...

等。

我通过循环遍历所有行来找到一个解决方案来构建这样的嵌套列表:

I found a solution by looping over all the rows to built nested lists like this:

years = [('1932-1940', [
                        ('Mid-summer', [Notice1, Notice2]),
                        ('September',  [Notice3])
                       ]),
           ('1950',    [
                        ('January', [Notice4])
                       ])
         ]

这是视图中的代码:

years = []
year = []
period = []
prev_year = ''
prev_period = ''
for notice in Notice.objects.all():
    if notice.year != prev_year:
        prev_year = notice.year
        year = []
        years.append((prev_year.name, year))
        prev_period = ''
    if notice.periode != prev_period:
        prev_period = notice.periode
        period = []
        if prev_period:
            name = prev_period.name
        else:
            name = None
        year.append((name, period))
    period.append(notice)

但这是缓慢而不实际的。这样做的好方法是什么?

But this is slow and inelegant. What's the good way to do this ?

如果我可以在模板中设置一些变量,我只能遍历所有的通知,只能通过检查是否打印年份和期间它们与前一个相同。但是不可能在模板中设置一些临时变量。

If I could set some variables inside the template I could only iterate over all the notices and only print the year and period by checking if they are the same as the previous one. But it's not possible to set some some temp variables in templates.

推荐答案

幸运的是Django有一些内置的模板标签,可以帮助您。可能主要的是重组

Luckily Django has some built-in template tags that will help you. Probably the main one you want is regroup:

{% regroup notices by year as year_list %}


{% for year in year_list %}
  <h2>{{ year.grouper }}<h2>

  <ul>
  {% for notice in year.list %}
     <li>{{ notice.text }}</li>
  {% endfor %}
  </ul>
{% endfor %}

还有 {%ifchanged% } ,当一个值保持不变时,这可以帮助循环列表。

There's also {% ifchanged %}, which can help with looping over lists when one value stays the same.

这篇关于在Django中,如何按年份显示对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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