我需要一个日历来显示时间表上的条目。我正在使用this guide编写django日历,但它没有涵盖最后阶段,也就是说,实际上将url传递给视图并渲染日历。基于有根据的猜测出错了,我的 urlconf 条目现在变成了这个怪物:
url(r'^calendar/(?P<pk>\d+)/(?P<start__year>\d+)/(?P<start__month>\d+)/$', calendar(request, year, month)),
因此,视图本身是:
def calendar(request, year, month):
my_timesheet = Timesheet.objects.order_by('start').filter(start__year=year, start__month=month)
cal = TimesheetCalendar(my_timesheet).formatmonth(year, month)
return render_to_response('calendar.html', {'calendar':mark_safe(cal),})
并且日历生成是:
class TimesheetCalendar(HTMLCalendar):
def __init__(self, Timesheet):
super(TimesheetCalendar, self).__init__()
self.Timesheet = self.group_by_day(Timesheet)
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
cssclass += ' today'
if day in self.Timesheet:
cssclass += ' filled'
body = ['<ul>']
for timesheet in self.Timesheet[day]:
body.append('<li>')
body.append(esc(Timesheet.activity))
body.append('</li>')
body.append('</ul>')
return self.day_cell(cssclass, '%d %s' % (day, ''.join(body)))
return self.day_cell(cssclass,day)
return self.daycell('noday',' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(TimesheetCalendar, self).formatmonth(year, month)
def group_by_day(self, Timesheet):
field = lambda Timesheet: Timesheet.start.day
return dict(
[(day, list(items)) for day, items in groupby(Timesheet, field)]
)
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' %(cssclass, body)
我必须怎么做才能从我的模型中的日期字段中正确传递这些属性(月份和年份)start
?