Rowan Ingram
Rowan Ingram

Reputation: 201

Django Calendar View - How to Pass the URL

I need a calendar to display entries on a timesheet. I was using this guide to writing a django calendar, but it does not cover the final stage, that is to say, actually passing the url to a view and rendering the calendar. Based on an educated guess gone wrong, my urlconf entry has now become this monstrosity:

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

The view itself is thus:

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),})

and the calender generation is:

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)

What must I do to correctly pass those attributes, month and year, from the datefield start in my model?

Upvotes: 0

Views: 759

Answers (1)

dm03514
dm03514

Reputation: 55962

I think second argument for url should be either a string representing your function or a function object:

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

Additionally, when you used named groups in your url (ie. start__year, start_month) they are passed to your view function as kwargs not as positional arguments

def calendar(request, pk, start__year, start__month):
    pass

Upvotes: 1

Related Questions