views:

40

answers:

1

I am trying to incorporate django-schedule into my project. Django-schedule's source is here. I don't like the urls, because they all capture a slug. My project will only allow one calendar per user, so it doesn't make sense to capture the slug. So, I wrapped the django-schedule views like this (look up the slug using the current user, and pass it to django-schedule's views):

from schedule.views import calendar_by_periods
from schedule.models import Calendar
from schedule.periods import Month

def cal_by_periods_wrapper(view):
    def new_view(request, *args, **kwargs):
        kwargs['calendar_slug'] = Calendar.objects.get_calendars_for_object(obj=request.user, distinction="owner")[0].slug
        return view(request, *args, **kwargs)
    return new_view

And here is the relevant section from urls.py:

urlpatterns = patterns('',
                url(r'^$',
                    cal_by_periods_wrapper(calendar_by_periods),
                           name = "month_calendar",
                           kwargs={'periods': [Month], 'template_name': 'schedule/calendar_month.html'}),

This works fine until it hits one of the template tags included with django-schedule, prev_url:

@register.simple_tag
def prev_url(target, slug, period):
    return '%s%s' % (
        reverse(target, kwargs=dict(calendar_slug=slug)),
            querystring_for_date(period.prev().start))

This function raises:

TemplateSyntaxError at /teacher/calendar/

Caught an exception while rendering: Reverse for 'month_calendar' with arguments 
'()' and keyword arguments '{'calendar_slug': u'asdf'}' not found.

How can I wrap this view and still make the reverse call work?

+1  A: 

This has nothing to do with wrapping the function. It's just that you no longer have a URL with the name 'month_calendar' which takes a 'calendar_slug' argument. Either define one in your urlconf, or edit the templatetag.

Edit after comment Yes but the 'reverse' call is still passing a slug argument, and there's no 'month_calendar' url which takes one, so the reverse match fails.

Daniel Roseman
I thought that's what name = "month_calendar" in my urls.py was for... and the point is that I'm trying to get rid of the calendar_slug from the url. Am I missing something?
jobrahms
See my edit above.
Daniel Roseman
Ok, sorry, this is making sense to me now. I thought the arguments it was complaining about were the args to the view function, not the URL. So, really, my only option is to modify the template tags to not pass in slugs. Got it. Thank you.
jobrahms