I am using Django DeleteView in a template and I've created a url & view. Is it possible to skip the process of loading the _confirm_delete template and just post the delete immediately.
6 Answers
DeleteView
responds to POST
and GET
requests, GET
request display confirmation template, while POST
deletes instance.
You can send POST
request, without confirmation with form like this:
<form method="POST" action="{% url "your_delete_url_name" %}">
{% csrf_token %}<input type="submit" value="DELETE">
</form>
If you do not want to have a link instead form button, use some javascript to make invisible form, that will be submitted on link click.
It is not good practice to use GET
request for updating or deleting, but if you really insist you can shortcut get
method in your class view to post
, ie:
def get(self, *args, **kwargs):
return self.post(*args, **kwargs)

- 6,233
- 34
- 45
-
1In Django 4.x.x it seems required to also add: ``` def post(self, request, *args, **kwargs): self.object = self.get_object() self.object.delete() return redirect(self.get_success_url()) ``` – scorpionipx Jan 11 '23 at 13:39
Or you can redefine get()
method in your DeleteView
:
class YourDeleteView(DeleteView):
model = YourModel
success_url = '<success_url>'
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
But be careful with that, ensure that this doesn't affect other functionality.

- 1,089
- 1
- 12
- 26
Yes, just change the next parameter. In your return response, make sure that the dictionary that you pass in is has something like this : { 'next': '/<your_path_here>}/' }
, make sure you commit the changes before adding the next
parameter. You might want to change your view's get
and post
functions.

- 80,178
- 33
- 141
- 199
-
I don't have any get or post views, everything generate with the DeleteView – yaniv14 Jul 04 '13 at 17:16
-
Okay then, well I do not think you can change it without changing your `post` function, and there is no way to do this as of 1.5 (to the best of my knowledge) looking at the documentation: https://docs.djangoproject.com/en/1.5/ref/class-based-views/flattened-index/#deleteview. If you do not know how to use `get()` and `post()` functions inside CBVs, then I suggest you learn about them. – Games Brainiac Jul 04 '13 at 17:21
Or you could only allow HTTP request method delete
by routing the request directly to the delete
method of your class.
from django.views.generic import DeleteView
from django.http import HttpResponseForbidden
class PostDeleteView(DeleteView):
model = Post
http_method_names = ['delete']
def dispatch(self, request, *args, **kwargs):
# safety checks go here ex: is user allowed to delete?
if request.user.username != kwargs['username']:
return HttpResponseForbidden()
else:
handler = getattr(self, 'delete')
return handler(request, *args, **kwargs)
def get_success_url(self):
username = self.kwargs.get('username')
success_url = str(reverse_lazy('post:user_home', kwargs={'username': username}))
return success_url
Let's say your URL looks like this:
path('posts/delete/<int:pk>/', PostDeleteView.as_view(), name='post_delete'),
For clarity why this works, you have to analyze the post
and delete
methods.
def post(self, request, *args, **kwargs):
return self.delete(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
"""
Call the delete() method on the fetched object and then redirect to the
success URL.
"""
self.object = self.get_object()
success_url = self.get_success_url()
self.object.delete()
return HttpResponseRedirect(success_url)
post
just calls delete
and delete
gets the object and success URL, deletes the object, then redirects to the success URL you provided. pk_url_kwarg = 'pk'
is why I showed the <int:pk>
part in the URL.

- 17,409
- 19
- 95
- 154
You can override the get()
method to behave exactly like the delete()
method:
def get(self, request, *args, **kwargs):
return self.delete(request, *args, **kwargs)
See CCBV here: https://ccbv.co.uk/projects/Django/4.1/django.views.generic.edit/DeleteView/

- 2,347
- 1
- 30
- 40
All you have to do is override the get_success_url
method of your delete view. Then it will directly delete the object from the DB. Eg:
class YourView(DeleteView):
model = YourModel
def get_success_url(self):
return reverse('your_redirect_view')

- 1,738
- 4
- 29
- 37

- 268
- 2
- 3