Bob's Blog

Web开发、测试框架、自动化平台、APP开发、机器学习等

返回上页首页

Django添加自定义的404等错误页面



当Django的settings里debug为True时,本地调试进入不存在的页面之类的操作,会提示路由错误,或者500错误时提示出错的代码位置。

当settings里debug为false时,只有简单的提示,比如404是显示文字(Not Found The requested resource was not found on this server.),403和500的错误也是一样。

这种默认的无样式的提示对于要发布的网站则不怎么友好,于是往往自己定义新的页面,但需要让django在遇到对应的错误类型时显示对应的html文件。模板文件的路径不同,我们会有不同的处理。

首先添加一个404的html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>404 not found</title>
    <!--template from https://freefrontend.com/html-css-404-page-templates/ -->
    <style>
        @import url('https://fonts.googleapis.com/css?family=Montserrat:400,600,700');
        @import url('https://fonts.googleapis.com/css?family=Catamaran:400,800');
        .error-container {
          text-align: center;
          font-size: 180px;
          font-family: 'Catamaran', sans-serif;
          font-weight: 800;
          margin: 20px 15px;
        }
        .error-container > span {
          display: inline-block;
          line-height: 0.7;
          position: relative;
          color: #FFB485;
        }
        .error-container > span {
          display: inline-block;
          position: relative;
          vertical-align: middle;
        }
        .error-container > span:nth-of-type(1) {
          color: #D1F2A5;
          animation: colordancing 4s infinite;
        }
        .error-container > span:nth-of-type(3) {
          color: #F56991;
          animation: colordancing2 4s infinite;
        }
        .error-container > span:nth-of-type(2) {
          width: 120px;
          height: 120px;
          border-radius: 999px;
        }
        .error-container > span:nth-of-type(2):before,
        .error-container > span:nth-of-type(2):after {
            border-radius: 0%;
            content:"";
            position: absolute;
            top: 0; left: 0;
            width: inherit; height: inherit;
          border-radius: 999px;
            box-shadow: inset 30px 0 0 rgba(209, 242, 165, 0.4),
                        inset 0 30px 0 rgba(239, 250, 180, 0.4),
                        inset -30px 0 0 rgba(255, 196, 140, 0.4),
                        inset 0 -30px 0 rgba(245, 105, 145, 0.4);
          animation: shadowsdancing 4s infinite;
        }
        .error-container > span:nth-of-type(2):before {
            -webkit-transform: rotate(45deg);
               -moz-transform: rotate(45deg);
                    transform: rotate(45deg);
        }

        .screen-reader-text {
            position: absolute;
            top: -9999em;
            left: -9999em;
        }
        @keyframes shadowsdancing {
          0% {
            box-shadow: inset 30px 0 0 rgba(209, 242, 165, 0.4),
                        inset 0 30px 0 rgba(239, 250, 180, 0.4),
                        inset -30px 0 0 rgba(255, 196, 140, 0.4),
                        inset 0 -30px 0 rgba(245, 105, 145, 0.4);
          }
          25% {
            box-shadow: inset 30px 0 0 rgba(245, 105, 145, 0.4),
                        inset 0 30px 0 rgba(209, 242, 165, 0.4),
                        inset -30px 0 0 rgba(239, 250, 180, 0.4),
                        inset 0 -30px 0 rgba(255, 196, 140, 0.4);
          }
          50% {
             box-shadow: inset 30px 0 0 rgba(255, 196, 140, 0.4),
                        inset 0 30px 0 rgba(245, 105, 145, 0.4),
                        inset -30px 0 0 rgba(209, 242, 165, 0.4),
                        inset 0 -30px 0 rgba(239, 250, 180, 0.4);
          }
          75% {
           box-shadow: inset 30px 0 0 rgba(239, 250, 180, 0.4),
                        inset 0 30px 0 rgba(255, 196, 140, 0.4),
                        inset -30px 0 0 rgba(245, 105, 145, 0.4),
                        inset 0 -30px 0 rgba(209, 242, 165, 0.4);
          }
          100% {
            box-shadow: inset 30px 0 0 rgba(209, 242, 165, 0.4),
                        inset 0 30px 0 rgba(239, 250, 180, 0.4),
                        inset -30px 0 0 rgba(255, 196, 140, 0.4),
                        inset 0 -30px 0 rgba(245, 105, 145, 0.4);
          }
        }
        @keyframes colordancing {
          0% {
            color: #D1F2A5;
          }
          25% {
            color: #F56991;
          }
          50% {
            color: #FFC48C;
          }
          75% {
            color: #EFFAB4;
          }
          100% {
            color: #D1F2A5;
          }
        }
        @keyframes colordancing2 {
          0% {
            color: #FFC48C;
          }
          25% {
            color: #EFFAB4;
          }
          50% {
            color: #D1F2A5;
          }
          75% {
            color: #F56991;
          }
          100% {
            color: #FFC48C;
          }
        }

        /* demo stuff */
        * {
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }
        body {
          background-color: #416475;
          margin-bottom: 50px;
        }
        html, button, input, select, textarea {
            font-family: 'Montserrat', Helvetica, sans-serif;
            color: #92a4ad;
        }
        h1 {
          text-align: center;
          margin: 30px 15px;
        }
        .zoom-area {
          max-width: 490px;
          margin: 30px auto 30px;
          font-size: 19px;
          text-align: center;
        }
        .link-container {
          text-align: center;
        }
        a.more-link {
          text-transform: uppercase;
          font-size: 13px;
            background-color: #92a4ad;
            padding: 10px 15px;
            border-radius: 0;
            color: #416475;
            display: inline-block;
            margin-right: 5px;
            margin-bottom: 5px;
            line-height: 1.5;
            text-decoration: none;
          margin-top: 50px;
          letter-spacing: 1px;
        }
    </style>
</head>
<body>
<section class="error-container">
  <span>4</span>
  <span><span class="screen-reader-text">0</span></span>
  <span>4</span>
  <p class="zoom-area"><b>找不到页面,请检查访问地址是否有误</b></p>
</section>

<div class="link-container">
  <a href="/" class="more-link">回到主页</a>
  <a href="javascript:history.go(-1)" class="more-link">返回上页</a>
</div>
</body>
</html>

1. 若文件直接命名为404.html, 403.html, 500.html这样的,且放在settings设置templates的根目录下,那么不需要我们做任何其他事。

首先检查templates在settings中的定义。

比如默认是是在代码目录下的templates文件夹:

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    },
]

于是只要将这几个html文件(注意名字)放在这个目录下,重启django即可。

2. 若需要单独存放并管理异常页面,则需要修改几个文件。

比如在templates目录下新建errors目录,将这几个html文件放进error文件夹内。

在对应app的views.py里新增几个方法,对应render这几个html:

def page_not_found(request, exception):
    return render(request, 'error/404.html')


def permission_denied(request, exception):
    return render(request, 'error/403.html')


def internal_error(request):
    return render(request, 'error/500.html')

然后在项目的urls.py中添加如下内容以指定:

from django.conf.urls import handler500, handler404, handler403
from blog import views as blog_views

handler403 = blog_views.permission_denied
handler404 = blog_views.page_not_found
handler500 = blog_views.internal_error

此时重启django即可。

需要注意的是,用下面的是不行的,会提示类似The custom handler404 view 'blog.views.page_not_found' does not take the correct number of arguments (request, exception)。这是因为新版的django对于非500错误是需要在参数中加上exception。像上面的代码那样。

# 错误的,会报错,缺少exception
def page_not_found(request):
    return render(request, 'error/404.html')

def permission_denied(request):
    return render(request, 'error/403.html')

 

下一篇:  mongodb的备份和恢复
上一篇:  Python与C扩展的几种方式

共有0条评论

添加评论

暂无评论