【发布时间】:2019-06-06 05:08:46
【问题描述】:
我已经尝试过如何在 CreateView 中检查(使用 get() 函数)。因此,当我们尝试访问 CreateView URL 时,此视图将检查是否已创建对象。如果是,它将重定向到该对象的 UpdateView URL。但问题是我不知道如何扭转它。
urls.py
app_name = 'product'
urlpatterns = [
url(r'^$', pglist, name='list'),
url(r'^create/$', pcreate, name='create'),
url(r'^(?P<slug>[\w-]+)/$', pdetail, name='detail'),
url(r'^(?P<slug>[\w-]+)/update/$', pupdate, name='update'),
url(r'^redire/$', ered, name='redire'),
views.py
CreateView class
def get(self, *args, **kwargs):
if self.model.objects.get(user=self.request.user):
return redirect("product:update", kwargs={'slug': ??? I HAVE NO IDEA HOW TO DOING THIS PART ???slug})
else:
return redirect("product:create")
如果我把这行改成 ==> return redirect("pages:update"), CreateView URL show
NoReverseMatch at /create/
Reverse for 'update' with no arguments not found. 1 pattern(s) tried: ['(?P<slug>[\\w-]+)/update/$']
那么,它应该是什么?
return redirect("product:update", kwargs={'slug': ??? I HAVE NO IDEA HOW TO DOING THIS PART ???slug})
【问题讨论】:
标签: django django-views