Django error page custom global 403,404,500

Original Address: https://www.jianshu.com/p/c5ecbd8cdbd3

Django error page custom global 403,404,500

Custom Templates

403

<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>403-禁止访问</title> </head> <body> HTTP 403 - 禁止访问 </body> </html> 

404

<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>404-无法找到文件</title> </head> <body> HTTP 404- 无法找到文件 </body> </html> 

500

<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>500-服务器错误</title> </head> <body> HTTP 500 - 内部服务器错误 </body> </html> 

Preparation of view

# 全局403、404、500错误自定义页面显示
def page_not_found(request): return render(request, '404.html') def page_error(request): return render(request, '500.html') def permission_denied(request): return render(request, '403.html') 

Modify url

from .views import page_error, page_not_found, permission_denied


urlpatterns = [
    # ...
]

# 定义错误跳转页面
handler403 = permission_denied
handler404 = page_not_found
handler500 = page_error

Try to use the non-privileged user access, see whether it will display the page

If not, modify the value of the DEBUG settings.py

DEBUG = False

Note: If DEBUG = True, in some cases it will not take effect

Http404 thrown

raise Http404('资源不存在<id:{}>,请访问 xxx 查看')

Template catch the exception information

Used {{ exception }}to capture the abnormality information, to convert the html code {{ exception|safe }}, according to the id code, etc., to obtain a link jump, reference

<!DOCTYPE html>
{% load static %}
<html lang="en"> <style type="text/css"> .pic { margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; } </style> <head> <meta charset="UTF-8"> <title>404-无法找到文件</title> <link href="//cdn.bootcss.com/toastr.js/latest/css/toastr.min.css" rel="stylesheet"> </head> <body> <a href="//blog.starmeow.cn"><img class="pic" src="{% static 'errors/404.gif' %}"></a> <p hidden>{{ exception|safe }}</p> <script src="//code.jquery.com/jquery-3.1.1.min.js"></script> <script src="//cdn.bootcss.com/toastr.js/latest/js/toastr.min.js"></script> <script> toastr.options = { // toastr配置 "closeButton": true, "debug": false, "progressBar": true, "positionClass": "toast-top-center", "showDuration": "400", "hideDuration": "1000", "timeOut": "7000", "extendedTimeOut": "1000", "showEasing": "swing", "hideEasing": "linear", "showMethod": "fadeIn", "hideMethod": "fadeOut" }; $(function () { let redirect_url = $('#redirect_url').text(); if (redirect_url.indexOf('//') === 0 || redirect_url.indexOf('http') === 0) { // 一链接开头才跳转 toastr.warning('{{ exception|safe }}', '跳转中'); setTimeout(function () { //这里写时间到后执行的代码 $(location).attr('href', redirect_url); }, 3000); } }) </script> </body> </html> 

rear end

raise Http404('访问资源不存在,即将跳转 <span id="redirect_url">{}</span>'.format('blog.starmeow.cn'))

So when there is a 404 error, jquery to get the value di, if it is //or is httpbeginning, suggesting that might be a link (please limit the back-end format), jump directly to the front



Author: I meow Star
link: https: //www.jianshu.com/p/c5ecbd8cdbd3
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/linwenbin/p/11389002.html