English 中文(简体)
Django - reversing wrapped view functions
原标题:

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?

最佳回答

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.

问题回答

暂无回答




相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签