對于健身房時間表,我需要創建一個模型,我將使用該模型獲得包含 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
提交
取消