如何在django的模板中仅显示列表中的不同值?

时间:2012-05-22 04:49:35

标签: python django list templates distinct

def avail_times(request, club_id):
    club = get_object_or_404(Club,pk=club_id)
    open_slots = Opening.objects.filter(club_id=club_id, day=datetime.date.today(), reservation ='Open')
    return render_to_response('reserve/templates/avail_times.html', {'club':club, 'open_slots':open_slots})


<ul>
{% for slot in open_slots %}
    <li> {{slot }} on Court {{slot.court}} </li>
{% endfor %}
</ul>


from django.db import models
import datetime

class Club(models.Model):
    establishment = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    def __unicode__(self):
        return self.establishment

class Day(models.Model):
    club = models.ForeignKey(Club)
    day = models.DateField('day')
    def __unicode__(self):
        return unicode(self.day)

class Court(models.Model):
    club = models.ForeignKey(Club)
    day = models.ForeignKey(Day)
    court = models.IntegerField(max_length=200)
    def __unicode__(self):
        return unicode(self.court)

class Opening(models.Model):
    club = models.ForeignKey(Club)
    day = models.DateField('date')
    court = models.ForeignKey(Court)
    slot = models.TimeField('slot')
    reservation = models.CharField(max_length=200)
    def __unicode__(self):
        return unicode(self.slot)

在上面的模板中,我只想显示一个明显的“插槽”时间。所以,即使早上6点在“球场1”和“球场2”下发生,我希望它只通过模板出现一次。我怎么做?

1 个答案:

答案 0 :(得分:6)

内置的regroup过滤器就是这样做的。