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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With