Django clalendar(HTMLCalendar)错误

时间:2014-05-14 09:44:23

标签: django

我有以下错误:我不知道,什么是必须整数,我是从日历导入HTMLCalendar,从日期时间导入日期

Request URL:http://article/calendar/2014/05/
Django Version: 1.6.2
Exception Type: TypeError
Exception Value: list indices must be integers, not unicode

我的Urls.py就在这里:

url(r"^calendar/$", "article.views.calendar"),
url(r"^calendar/(?P<year>\d+)/(?P<month>\d+)/$", "article.views.calendar"),

这是Views.py:

class WorkoutCalendar(HTMLCalendar):

    def __init__(self, workouts):
        super(WorkoutCalendar, self).__init__()
        self.workouts = self.group_by_day(workouts)

    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.workouts:
                cssclass += ' filled'
                body = ['<ul>']
                for workout in self.workouts[day]:
                    body.append('<li>')
                    body.append('<a href="%s">' % workout.get_absolute_url())
                    body.append(esc(workout.title))
                    body.append('</a></li>')
                body.append('</ul>')
                return self.day_cell(cssclass, '%d %s' % (day, ''.join(body)))
            return self.day_cell(cssclass, day)
        return self.day_cell('noday', '&nbsp;')

    def formatmonth(self, year, month):
        self.year, self.month = year, month
        return super(WorkoutCalendar, self).formatmonth(year, month)

    def group_by_day(self, workouts):
        field = lambda workout: staff.created_at.day
        return dict(
            [(day, list(items)) for day, items in groupby(workouts, field)])

    def day_cell(self, cssclass, body):
        return '<td class="%s">%s</td>' % (cssclass, body)

def calendar(request, year, month):
    my_workouts = Staff.objects.order_by('created_at').filter(created_at__year=year,    created_at__month=month)
    cal = WorkoutCalendar(my_workouts).formatmonth(year, month)
    return render_to_response('home.html', {'calendar': mark_safe(cal),})

0 个答案:

没有答案