Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django class-based view - DeleteView - How to disable confirmation requirement

I am switching to the class-based views. I also use JavaScript to confirm any deletion on the client side. Django DeleteView requires a delete confirmation template which I don't care about.

Is there any simple way of disabling the confirmation on any kind of deletes in Django?

class EntryDeleteView(DeleteView):
    model = Entry
    success_url = reverse_lazy('entry_list')   # go back to the list on successful del
    template_name = 'profiles/entry_list.html' # go back to the list on successful del

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(EntryDeleteView, self).dispatch(*args, **kwargs)
like image 382
un33k Avatar asked Jan 18 '23 08:01

un33k


2 Answers

You should make a POST query from clientside (with AJAX or POSTing a form). That's because if you'll allow to delete something by GET, your service will be vulnerable to CSRF. Someone will send your admin a in email or somehow else, and you'll be in trouble.

like image 150
ilvar Avatar answered Apr 06 '23 21:04

ilvar


The DeleteView renders the confirmation page on GET and deletes the object if you use a POST or DELETE. If your JS does a POST to the url after confirmation it should work like you want.

like image 37
Mark Lavin Avatar answered Apr 06 '23 23:04

Mark Lavin