我有一个名为workspace的应用程序,
我的
中的form_valid定义中的某处def form_valid(self, form):
obj = form.instance
obj.owner = self.request.user
obj.sort_order = Project.default_sort_order()
obj.workspace = get_object_or_404(Workspace, pk=self.kwargs['pk'])
context = self.get_context_data()
attribute_form = context['attribute_formset']
if attribute_form.is_valid():
self.object = form.save()
for aform in attribute_form:
aform.instance.owner = self.request.user
attribute = aform.save()
self.object.attributes.add(attribute)
return HttpResponseRedirect(reverse(self.get_success_url()))
else:
return self.render_to_response(self.get_context_data(form=form))
和我对get_success_url的定义:
def get_success_url(self):
return reverse('workspace_project', kwargs={'pk':self.object.pk})
我的网址定义是:
url(r'^project/(?P<pk>\d+)$', ProjectDetailView.as_view(), name='workspace_project'),
包含在主urlconf中,如下所示:
url('^workspace/', include('workspace.urls')),
但是,只要创建成功,我就会收到此重定向错误:
NoReverseMatch at /workspace/workspace/3/create_project
Reverse for '/workspace/project/12' with arguments '()' and keyword arguments '{}' not found.
我访问了网址/workspace/project/12
,我可以访问它。为什么这个错误呢?
提前致谢!
答案 0 :(得分:2)
您使用reverse()两次的原因是什么?在get_success_url()和HttpResponseRedirect()中都有。您还可能想要引用self.object.get_success_url()。