删除对象 Django-代码看起来正确但不起作用?

发布于 2024-11-17 04:29:02 字数 595 浏览 1 评论 0原文

我已经在这方面工作太久了,尽管我进行了所有研究,但仍无法找出问题所在。不可否认,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2024-11-24 04:29:02

我假设你的缩进是正确的。

很确定你应该去< 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 an object you're deleting, not an entire queryset, you don't need the .all(). Actually, to delete multiple objects you'd write Ticket.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, only POST.

Lastly, you should use reverse for your redirect, or at least an absolute URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文