开发者

Django {% regroup %} produces duplicate groups

I've defined the following models:

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})

And use the following template to produce this page (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 %}

The view behind this is a generic one, and invoked as:

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

So, Projects are already sorted by Topic. Unfortunately, this code yields to duplicate gorups and, sometimes, the groups change at each refresh (or, at least, they change when I reboot the server).

Any idea of why is this happening? The entire code, besides templates', resides here: https://bitbucket.org/phretor/django-academic/src/


The dictsort filter is only for lists of dicts, you don't need it at all here. Your template should read

{% regroup object_list by topic as topic_list %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜