Django配置出错时候的模板

来源:互联网 发布:秦皇岛网络推广 编辑:程序博客网 时间:2024/05/17 23:23

一、URLconf下重新定义Django内置的handler函数:
mysite就是你的app


handler404 = 'mysite.views.my_custom_page_not_found_view'

HttpResponseNotFound,
handler500 = 'mysite.views.my_custom_error_view'

HttpResponseServerError,
handler403 = 'mysite.views.my_custom_permission_denied_view'

HttpResponseForbidden,
handler400 = 'mysite.views.my_custom_bad_request_view'

HttpResponseBadRequest

views中编写
返回是 return HttpresponseNotFound()

from django.http import HttpResponse.HttpresponseNotFound

出错的函数打log就好了 不要做复杂操作

二、自己建立404.html,500.html等放到公共Templates目录下

这两种方式都只在DEBUG=False时起作用,最方便的方式是采用第二种方式,而如果有特殊的需求,例如当页面出错时想自己处理,那么也可以自己定义handler view来处理。

0 0
原创粉丝点击