0

我需要一个日历来显示时间表上的条目。我正在使用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','&nbsp;')

    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

4

1 回答 1

1

我认为 for 的第二个参数url应该是代表您的函数的字符串或函数对象:

url(r'^calendar/(?P<pk>\d+)/(?P<start__year>\d+)/(?P<start__month>\d+)/$', calendar)

此外,当您在 url 中使用命名组(即start__year, start_month)时,它们kwargs不会作为位置参数传递给您的视图函数

def calendar(request, pk, start__year, start__month):
    pass
于 2013-08-12T16:18:41.973 回答