Reputation: 103
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+)$', 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.
Upvotes: 0
Views: 398
Reputation: 283325
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.
Upvotes: 4