2

我正在尝试将 django-schedule 合并到我的项目中。Django-schedule 的源代码在这里。我不喜欢这些网址,因为它们都捕获了一个蛞蝓。我的项目将只允许每个用户使用一个日历,因此捕获 sl 没有意义。因此,我像这样包装了 django-schedule 视图(使用当前用户查找 slug,并将其传递给 django-schedule 的视图):

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

这是 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'}),

这可以正常工作,直到它遇到 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))

此功能引发:

TemplateSyntaxError at /teacher/calendar/

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

我怎样才能包装这个视图并仍然使反向调用工作?

4

1 回答 1

1

这与包装函数无关。只是您不再有一个名为“month_calendar”的 URL,它带有一个“calendar_slug”参数。在您的 urlconf 中定义一个,或编辑模板标签。

评论后编辑是的,但“反向”调用仍在传递一个 slug 参数,并且没有“month_calendar”网址需要一个,因此反向匹配失败。

于 2010-01-19T20:59:17.970 回答