我试图为医生显示办公时间,并希望将同一天的工作时间放在一行,而不是让他们在不同的路线上。
例如,这就是它目前在模板中显示的方式
Sunday: 9 am - noon
Sunday: 1pm - 9pm
Monday: 8:30am - 11am
我希望它显示
Sunday: 9am - noon , 1pm - 9pm
Monday: 8:30am - 11am
我该怎么做?
WEEKDAYS = [
(1, ("Sunday")),
(2, ("Monday")),
(3, ("Tuesday")),
(4, ("Wednesday")),
(5, ("Thursday")),
(6, ("Friday")),
(7, ("Saturday")),
]
class OpeningHour(models.Model):
doctor = models.ForeignKey(Doctor)
weekday = models.IntegerField(choices=WEEKDAYS)
from_hour = models.TimeField(unique=False)
to_hour = models.TimeField(unique=False)
class Meta:
ordering = ('weekday', 'from_hour')
def __unicode__(self):
return u'%s: %s: %s - %s' % (self.doctor.name, self.get_weekday_display(),
self.from_hour, self.to_hour)
{% for a in Hours %}
<p>{{a.get_weekday_display}}: {{a.from_hour}} - {{a.to_hour}}</p>
{% endfor %}
hours = OpeningHour.objects.filter(doctor_id=id)
答案 0 :(得分:0)
假设模板中的hours
表示OpeningHour对象的有序列表(按工作日排序),则可以使用ifchanged模板标记。类似的东西:
{% for a in hours %}
{% ifchanged a.weekday %}
<p> {{a.get_weekday_display}}:
{% endifchanged %}
{{a.from_hour}} - {{a.to_hour}},
{% ifchanged a.weekday %}
</p>
{% endifchanged %}
{% endfor %}
(应该适应考虑到一些限制条件,但这是想法)