简体   繁体   English

模板中的Django格式数据库条目

[英]Django format database entry in template

I'm trying to show office hours for a doctor and would like to have the hours for the same day in one line, instead of having them on different lines. 我正在尝试为医生显示办公时间,并且希望将同一天的时间安排在一条线上,而不是将它们放在不同的线路上。

For instance, this is how it's currently showed in the templates 例如,这是当前在模板中显示的方式

Sunday: 9 am - noon
Sunday: 1pm - 9pm
Monday: 8:30am - 11am

I want it to be shown 我希望它被显示

Sunday: 9am - noon , 1pm - 9pm
Monday: 8:30am - 11am

How can I do that? 我怎样才能做到这一点?

models.py models.py

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)

template.py template.py

 {% for a in Hours %}
                <p>{{a.get_weekday_display}}: {{a.from_hour}} - {{a.to_hour}}</p>
            {% endfor %}

hours list 小时表

在此处输入图片说明

views.py views.py

hours = OpeningHour.objects.filter(doctor_id=id)

Assuming the hours in the template represent an ordered list of OpeningHour objects (ordered by weekday), then you can use the ifchanged template tag. 假设模板中的hours代表OpeningHour对象的有序列表(按工作日排序),则可以使用ifchanged模板标记。 Something like: 就像是:

{% 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 %}

(should probably be adapted to take into account some limit conditions, but this is the idea) (可能应该考虑到一些极限条件,但这是一个主意)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM