How to display a custom error page for HTTP status 405 (method not allowed) in Django when using @require_POST

前端 未结 3 1144
孤独总比滥情好
孤独总比滥情好 2021-02-05 20:40

My question is simple, how do I display a custom error page for HTTP status 405 (method not allowed) in Django when using the @require_POST decorator?

I\'m

3条回答
  •  孤街浪徒
    2021-02-05 20:58

    If you look into the documentation and the source code of django.views.defaults you see that only 404 and 500 errors are supported in a way that you only have to add the 404.html resp. 500.html to your templates directory.

    In the doc. you can also read the following

    Returning HTTP error codes in Django is easy. There are subclasses of HttpResponse for a number of common HTTP status codes other than 200 (which means "OK"). You can find the full list of available subclasses in the request/response documentation.

    Thus if you want to return a 405 error, you have to use the HttpResponseNotAllowed class

    An example

提交回复
热议问题