原文地址:https://www.jianshu.com/p/c5ecbd8cdbd3
尝试使用无权限用户访问,看是否会显示该页面
如果不对,修改settings.py中的DEBUG的值
DEBUG = False
注:若是DEBUG=True,有些情况下则不会生效
使用{{ exception }}即可捕获异常信息,转换为html代码{{ exception|safe }},可以根据这些代码中的id等,得到跳转的链接,参考
<!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>后端
raise Http404('访问资源不存在,即将跳转 <span id="redirect_url">{}</span>'.format('blog.starmeow.cn'))那么当出现404错误是,jquery就获取该di的值,如果是//或者是http开头,表明可能是个链接(后端请限制格式),前端直接跳转
作者:吾星喵 链接:https://www.jianshu.com/p/c5ecbd8cdbd3 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。转载于:https://www.cnblogs.com/linwenbin/p/11389002.html
