Python Django HTMLCalendar 如何将第一天设置为星期日
就像标题所说的,目标是让日历中的一周从星期天开始,而不是像下面那样从星期一开始。我知道可以用 calendar.setfirstweekday(calendar.SUNDAY)
来实现这个功能。可是我就是搞不清楚该怎么把它加到这段代码里,或者是否需要其他的方法。我不想浪费你们的时间,之前尝试过的一些方法都很傻。
当前显示星期一为周首的代码:
class Calendar(HTMLCalendar):
def __init__(self, year=None, month=None, group=None):
self.year = year
self.month = month
self.group = group
super(Calendar, self).__init__()
def formatday(self, day):
d = ''
if day != 0:
return f"<td name='date-event-input' id={date(year=self.year, month=self.month, day=day)}><span " \
f"class='date'>{day}</span><ul>{d}</ul></td>"
return '<td></td>'
def formatweek(self, theweek):
week = ''
for d, weekday in theweek:
week += self.formatday(d)
return f'<tr> {week} </tr>'
def formatmonth(self, withyear=True):
cal = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar" id="calendar-table-id">\n'
cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
cal += f'{self.formatweekheader()}\n'
for week in self.monthdays2calendar(self.year, self.month):
cal += f'{self.formatweek(week)}\n'
return cal
非常感谢任何帮助
1 个回答
1
你可以在创建自定义日历类的实例之后立即设置它。这样应该没问题。我的意思是:
# Create an instance of your custom calendar class
my_calendar = Calendar()
# Set the first weekday to Sunday
calendar.setfirstweekday(6)