美文网首页
Django之自定义403/404/500页面

Django之自定义403/404/500页面

作者: _Cappuccino_ | 来源:发表于2019-05-10 11:23 被阅读0次

    跟路由设置

    # 全局403
    handler403 = 'logs.views.page_forbidden'
    
    # 全局404
    handler404 = 'logs.views.page_not_found'
    
    # 全局500
    handler500 = 'logs.views.page_error'
    

    logs.views中的代码:

    from django.shortcuts import render_to_response, render
    
    def page_forbidden(request, exception):
        response = render_to_response("403.html", {})
        response.status_code = 403
        return response
    
    def page_not_found(request, exception):
        response = render_to_response("404.html", {})
        response.status_code = 404
        return response
    
    def page_error(exception):
        response = render_to_response("500.html", {})
        response.status_code = 500
        return response
    

    需要注意的,400和403页面需要传递request参数和exception参数,而500页面是不需要reqeust参数的!

    相关文章

      网友评论

          本文标题:Django之自定义403/404/500页面

          本文链接:https://www.haomeiwen.com/subject/ghiooqtx.html