Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django DeleteView without confirmation template

Tags:

python

django

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.

like image 995
yaniv14 Avatar asked Jul 04 '13 17:07

yaniv14


2 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) 
like image 153
bmihelac Avatar answered Sep 19 '22 14:09

bmihelac


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.

like image 36
AmirM Avatar answered Sep 19 '22 14:09

AmirM