网上的做法基本都是下面的代码
return HttpResponseForbidden()
试了一下,效果一般,没有异常页面显示,最终显示的是浏览器的异常页面,如下图:
如果要想让服务器截获异常并且显示错误页可以用下面的方式:
id = request.GET.get('id', '')
timestamp = request.GET.get('timestamp', '')
accesskey = request.GET.get('accesskey', '')
if timestamp == '' or accesskey == '' or id == '':
raise PermissionDenied
此时访问就会显示正常的错误页了,如下
django 错误页面定义方法:
urls.py加入如下代码:
handler403 = permission_denied
handler404 = page_not_found
handler500 = page_error
相关view代码:
# Create your views here.
def page_not_found(request):
return HttpResponseRedirect('/static/error_pages/404.html')
def page_error(request):
return HttpResponseRedirect('/static/error_pages/500.html')
def permission_denied(request):
return HttpResponseRedirect('/static/error_pages/403.html')