删除对象 Django-代码看起来正确但不起作用?
我已经在这方面工作太久了,尽管我进行了所有研究,但仍无法找出问题所在。不可否认,我是 django 的新手。
我有一个票证应用程序,它使用表单来创建、编辑和更新票证。我现在尝试根据主键 Ticket_id 删除它们。
在我看来,我有:
def deleteTicket(request, ticket_id):
ticket = Ticket.objects.get(pk=ticket_id)
ticket.delete.all()
redirect_to = 'project/tickets.html'
return HttpResponseRedirect(redirect_to)
在我的 urls.py 中:
(r'^(?P<project_slug>[^\.^/]+)/tickets/(?P<ticket_id>\d+)$', views.deleteTicket),
当我打开票证并单击应调用此视图的链接时,会出现预期的页面,但我刚刚尝试删除的票证仍然列出。我没有在任何地方收到任何错误,但这段代码什么也没做。为什么?
感谢您的帮助!它一直让我发疯。
I have been working on this for far too long, and despite all of my research cannot find out what is wrong. Admittedly, I am new to django.
I have a ticket app that uses forms to create, edit and update tickets. I'm now trying to delete them, based on the ticket_id which is a primary key.
In my view I have:
def deleteTicket(request, ticket_id):
ticket = Ticket.objects.get(pk=ticket_id)
ticket.delete.all()
redirect_to = 'project/tickets.html'
return HttpResponseRedirect(redirect_to)
In my urls.py:
(r'^(?P<project_slug>[^\.^/]+)/tickets/(?P<ticket_id>\d+)
When I open a ticket and click on the link that should call this view the expected page appears but the ticket I just tried to delete is still listed. I'm not getting any errors anywhere, but this code is doing nothing. Why?
Thanks for your help! It has been driving me crazy.
, views.deleteTicket),
When I open a ticket and click on the link that should call this view the expected page appears but the ticket I just tried to delete is still listed. I'm not getting any errors anywhere, but this code is doing nothing. Why?
Thanks for your help! It has been driving me crazy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设你的缩进是正确的。
很确定你应该去< code>ticket.delete() 因为您要删除的是一个
对象
,而不是整个查询集,所以您不需要.all()
。实际上,要删除多个对象,您需要编写Ticket.objects.all().delete()
,因此您使用的语法完全错误,我很惊讶它没有抛出异常。此外,您不应通过
GET
请求删除,而只能通过POST
请求删除。最后,您应该使用
reverse< /code>
用于重定向,或者至少是绝对 URL。
I'm going to assume your indentation is correct.
Pretty sure you're just supposed to go
ticket.delete()
since it's anobject
you're deleting, not an entire queryset, you don't need the.all()
. Actually, to delete multiple objects you'd writeTicket.objects.all().delete()
, so the syntax you're using is just plain wrong and I'm surprised it's not throwing an exception.Also, you shouldn't delete via
GET
requests, onlyPOST
.Lastly, you should use
reverse
for your redirect, or at least an absolute URL.