Django - 反转包装视图函数

发布于 2024-08-18 14:07:48 字数 1554 浏览 4 评论 0原文

我正在尝试将 django-schedule 合并到我的项目中。 Django-schedule 的源代码位于此处。我不喜欢这些网址,因为它们都捕获了一个 slug。我的项目只允许每个用户一个日历,因此捕获 slug 没有意义。因此,我像这样包装了 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.

如何包装此视图并仍然使反向调用工作?

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'^

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?

, 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:

This function raises:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2024-08-25 14:07:48

这与包装函数无关。只是您不再拥有名为“month_calendar”且带有“calendar_slug”参数的 URL。要么在 urlconf 中定义一个,要么编辑 templatetag。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文