3

I want to raise or return a 405 Method not allowed but I cannot find how. Like in Flask you could do abort(405). Is there anyway to raise or return a Django 405 exception.

1

1 Answer 1

5

Ok, I found the answer. You could use what Alex has said. But you can also use the HttpResponseNotAllowed and click here for the docs.

return HttpResponseNotAllowed() 

If you want to return a custom one just do the following:

return render(request, "Error Pages/405.html", status=405) 

Notice the status, that is what will tell the browser about the code that is being returned.

Sign up to request clarification or add additional context in comments.

1 Comment

For 404 errors, you have a choice between throwing an Http404 exception and returning an HttpResponseNotFound object. There is no equivalent Http405 class though, only HttpResponseNotAllowed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.