Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: DeleteView + HttpResponseNotAllowed

Tags:

python

django

I found this DeleteView. Anyone can tell me what return HttpResponseNotAllowed(['POST'])is for? Should I add it to my own DeleteView as well?

class DiscountDelete(AdminPermissionRequiredMixin, DeleteView):
    model = Discount

    def get(self, *args, **kwargs):
        return HttpResponseNotAllowed(['POST'])
like image 283
Joey Coder Avatar asked Dec 11 '25 06:12

Joey Coder


2 Answers

With Django's class based views you can define a class variable for this;

class DiscountDelete(AdminPermissionRequiredMixin, DeleteView):
    model = Discount
    http_method_names = ['post']

Then if that view receives a get request it'll send back the 405 you're looking for.

Docs on this are here; https://docs.djangoproject.com/en/2.1/ref/class-based-views/base/#django.views.generic.base.View.http_method_names

like image 87
markwalker_ Avatar answered Dec 13 '25 19:12

markwalker_


With the usual delete view, when you do a GET request you get a confirmation page. Then when you submit the form with a POST request, the object is deleted.

The custom get() method is disabling GET requests. Perhaps it's not needed, because the delete requests are submitted from a different view (e.g. a list view).

We can't tell whether or not you should add this functionality to your delete view. It's up to you.

like image 25
Alasdair Avatar answered Dec 13 '25 19:12

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!