[Fixed]-Check in DeleteView if its post or get? django

0πŸ‘

βœ…

By default DeleteView does deletion only on POST request. So your user will not be able to delete items just making GET request.

But for your information all class based views(CBV) call dispatch method which then calls ether post or get depending on request.method.

You can add some logic directly in dispatch method or modify get and do your checks there

Example

class MyDeleteView(DeleteView):
    def post(self, request, *args, **kwargs):
        ...

    def get(self, request, *args, **kwargs):
        # here you can make redirect to any url
        ...

1πŸ‘

It should be a POST, there isn’t any need to check it yourself.

From the docs

The given object will only be deleted if the request method is POST. If this view is fetched via GET, it will display a confirmation page that should contain a form that POSTs to the same URL.

Leave a comment