reverse_lazy与kwargs不匹配

时间:2017-01-28 00:27:01

标签: django django-urls

我有一个Django CreateView应该重定向到查询字符串中带有UUID的URL:

@method_decorator(xframe_options_exempt, name='dispatch') 
class ActivityCreateView(CreateView):

    template_name = 'embedded/activities/create.html'
    form_class = ActivityCreateUpdateForm

    def get_success_url(self):
        return reverse_lazy('embedded:activity_status', kwargs={'unieke_code': self.object.access_link})

然而,这给出了熟悉的错误:

Reverse for 'activity_status' with arguments '()' and keyword arguments '{'unieke_code': UUID('470e3a5f-6f52-414e-a431-bf5c6e68509a')}' not found. 1 pattern(s) tried: ['embedded/activiteiten/status/']

奇怪的是,它显然找到了模式,但仍然不匹配。这是我的urls.py:

urlpatterns = [
    url(r'^activiteiten/$', views.ActivitiesIndexView.as_view(), name='activities_index'),
    url(r'^activiteiten/aanmelden/$', views.ActivityCreateView.as_view(), name='activity_create'),
    url(r'^activiteiten/status/', views.ActivityStatusView.as_view(), name='activity_status'),
]

奇怪的是,当我去mysite.com/embedded/activiteiten/status/?unieke_code=470e3a5f-6f52-414e-a431-bf5c6e68509a它确实有效。

1 个答案:

答案 0 :(得分:2)

reverse和reverse_lazy函数不用于传递GET参数。这就是它失败的原因。

这样的事情应该有效:

=

旁注:在Python 2中,您应该从urllib导入urlencode,在Python 3中,它在urllib.parse中