对于健身房时间表,我需要创建一个模型,我将使用该模型获得包含 7 天名称的列表。该列表将每天更新,因此首先将始终是一周中的当前日期。列表每天都会移到一天,例如,在“星期三”一词今天所在的单元格中,明天将出现“星期四”一词,后天 - “星期五”等等。然后应该将日期的名称检索到视图中并传递给模板。我试着这样做:import calendarimport datetimefrom datetime import *from django.db import modelsfrom django.utils.translation import ugettext as _class DayOfWeekSchedule(models.Model): """General dynamic schedule for a week""" DOW_CHOICES = ( (1, _("Monday")), (2, _("Tuesday")), (3, _("Wednesday")), (4, _("Thursday")), (5, _("Friday")), (6, _("Saturday")), (7, _("Sunday")), ) day_of_week = models.PositiveSmallIntegerField( choices=DOW_CHOICES, verbose_name=_('Day of week') ) def days_of_week(self): my_date = date.today() current_day_name = calendar.day_name[my_date.weekday()] index1 = DOW_CHOICES.index(current_day_name) #from Monday=0 (i.e. Friday=4) list_daynames = list(DOW_CHOICES[index1:] + DOW_CHOICES)[:7] #list of names, current is first list_index = (2, 3, 4, 5, 6, 7, 1) #to compensate systems difference list2 = list(list_index[index1:] + list_index)[:7] context_days = dict(zip(list_keys, list_daynames)) return context_days但DOW_CHOICES.index (current_day_name)和list_daynames = list (DOW_CHOICES [index1:] + DOW_CHOICES) [: 7],正如预期的那样,不起作用,因为 .index() 不适用于这种类型的元组。以及如何做到这一点,我不知道。
添加回答
举报
0/150
提交
取消