Her*_*aaf 11 django http-status-codes http-status-code-405
我的问题很简单,在使用@require_POST装饰器时,如何在Django中显示HTTP状态405(方法不允许)的自定义错误页面?
我正在使用django.views.decorators.http.require_POST装饰器,当GET请求访问页面时,控制台显示405错误,但页面只是空白(甚至不是Django错误页面).如何让Django显示此类错误的自定义和/或默认错误页面?
编辑:值得一提的是,我已经尝试在我的模板文件夹中放置一个404.html,500.html和405.html页面 - 但这也无济于事.我曾经也之间的变化DEBUG = True和False,都无济于事.
Vla*_*nco 13
你必须编写自定义Django中间件.您可以从这个开始并扩展它以检查405.html文件是否存在等等:
from django.http import HttpResponseNotAllowed
from django.template import RequestContext
from django.template import loader
class HttpResponseNotAllowedMiddleware(object):
def process_response(self, request, response):
if isinstance(response, HttpResponseNotAllowed):
context = RequestContext(request)
response.content = loader.render_to_string("405.html", context_instance=context)
return response
Run Code Online (Sandbox Code Playgroud)
如果您不知道如何安装中间件,请检查文档:
http://docs.djangoproject.com/en/dev/topics/http/middleware/
您还可以查看这篇文章:
http://mitchfournier.com/2010/07/12/show-a-custom-403-forbidden-error-page-in-django/
| 归档时间: |
|
| 查看次数: |
5716 次 |
| 最近记录: |