我尝试过如何在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显示
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})
答案 0 :(得分:0)
好吧,我冒昧地猜测您的模型上有一个slug
字段,您可以像这样向reverse
方法的缺点提供子弹:
return redirect(
"product:update",
kwargs={'slug': self.model.objects.get(user=self.request.user).slug}
)