Django {%regroup%}生成重复的组

时间:2011-07-03 22:47:12

标签: django django-models django-templates

我已经定义了以下模型:

class Topic(models.Model):
class Meta:
    ordering = [
        'title']

objects = models.Manager()
highlighted = HighlightedTopicManager()

highlight = models.BooleanField(
    default=False,
    help_text='Show this topic on the home page?',
    db_index=True)
highlight_order = models.PositiveSmallIntegerField(
    default=0,
    help_text='In what order do you want this to be added on the home page?'\
        ' Leave blank for alphabetic order.',
    db_index=True)
title = models.CharField(
    max_length=2048,
    db_index=True)
slug = models.SlugField(
    max_length=128,
    db_index=True)
excerpt = models.TextField(
    null=True,
    blank=True)
description = models.TextField()

def _get_content(self):
    if self.excerpt:
        return self.excerpt
    return self.description
content = property(_get_content)

@models.permalink
def get_absolute_url(self):
    return ('academic_projects_topic_detail', (), {'slug': self.slug})

def __unicode__(self):
    return self.title

class Project(models.Model):
class Meta:
    ordering = [
        'topic',
        'modified',
        'created']

objects = models.Manager()
highlighted = HighlightedProjectManager()

highlight = models.BooleanField(
    help_text='Highlight this in the projects\' main page?'\
        ' Only the most recently modified one will be displayed.')
redirect_to = models.URLField(
    blank=True,
    null=True,
    help_text='Use this for old or extenal projects.')
short_title = models.CharField(
    max_length=1024,
    db_index=True)
slug = models.SlugField(
    max_length=128,
    db_index=True)
title = models.CharField(
    max_length=2048,
    db_index=True)
created = models.DateTimeField(
    auto_now_add=True)
modified = models.DateTimeField(
    auto_now=True)
excerpt = models.CharField(
    max_length=1024,
    null=True,
    blank=True,
    help_text='Concise description to show in the listing page.')
description = models.TextField(
    null=True,
    blank=True,
    help_text='This content will be rendered right after the title.')
downloads = models.ManyToManyField(
    Download,
    null=True,
    blank=True,
    help_text='Downloadable files')
footer = models.TextField(
    null=True,
    blank=True,
    help_text='This content will be rendered at the bottom of the page.')
people = models.ManyToManyField(
    Person,
    help_text='People involved in this project.',
    related_name='projects')
organizations = models.ManyToManyField(
    Organization,
    help_text='Organizations involved other than the lab.',
    blank=True,
    null=True,
    related_name='projects')
publications = models.ManyToManyField(
    Publication,
    blank=True,
    null=True)
topic = models.ForeignKey(
    Topic,
    verbose_name=_('Main topic'),
    help_text='This is the main topic.',
    related_name='projects')
sponsors = models.ManyToManyField(
    Sponsor,
    blank=True,
    null=True,
    help_text='sponsored_projects')
related_topics = models.ManyToManyField(
    Topic,
    null=True,
    blank=True,
    help_text='Optional related topics.',
    related_name='secondary_projects')

def __unicode__(self):
    return self.short_title

@models.permalink
def get_absolute_url(self):
    return ('academic_projects_project_detail', (), {'slug': self.slug})

使用以下模板制作此页面(http://seclab.cs.ucsb.edu/academic/projects/):

{% extends "academic/project_base.html" %}

{% block content_title %}Projects{% endblock %}
{% block title %}Projects - {{block.super}}{% endblock %}

{% block content %}
  {% regroup object_list|dictsort:"topic" by topic as topic_list %}

  {% for topic in topic_list %}
  <h2 id="{{ topic.grouper.slug }}">{{ topic.grouper }} <a href="#{{ topic.grouper.slug }}">#</a></h2>
  {% for project in topic.list %}
    <h3><a href="{{ project.get_absolute_url }}">{{ project }}</a></h3>
    <p>{{ project.title }}</p>
  {% endfor %}
  {% endfor %}
{% endblock %}

这背后的观点是通用的,并调用为:

url(r'^$',
    cache_page(ListView.as_view(
            queryset=Project.objects.order_by('topic'),
            template_name='academic/project_list.html')),
    name='academic_projects_project_list'),

因此,Project已按Topic排序。不幸的是,这段代码会产生重​​复的gorup,有时,每次刷新时组都会发生变化(或者,至少,当我重新启动服务器时,它们会发生变化)。

知道为什么会这样吗?除了模板之外,整个代码都位于此处:https://bitbucket.org/phretor/django-academic/src/

1 个答案:

答案 0 :(得分:1)

dictsort过滤器仅用于dicts列表,您根本不需要它。您的模板应该是

{% regroup object_list by topic as topic_list %}