Django自定义全局403、404、500错误页面的示例代码


Posted in Python onMarch 08, 2020

自定义模板

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>

编写视图

# 全局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')

修改url

from .views import page_error, page_not_found, permission_denied


urlpatterns = [
 # ...
]

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

尝试使用无权限用户访问,看是否会显示该页面

如果不对,修改settings.py中的DEBUG的值

DEBUG = False

注:若是DEBUG=True,有些情况下则不会生效

Http404抛出异常

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

模板中捕获异常信息

使用{{ 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="external nofollow" rel="stylesheet">
</head>
<body>
<a href="//blog.starmeow.cn" rel="external nofollow" ><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开头,表明可能是个链接(后端请限制格式),前端直接跳转

到此这篇关于Django自定义全局403、404、500错误页面的示例代码的文章就介绍到这了,更多相关Django 403、404、500错误页面内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python BeautifulSoup中文乱码问题的2种解决方法
Apr 22 Python
python简单的函数定义和用法实例
May 07 Python
Python 3.x 新特性及10大变化
Jun 12 Python
详解Python中的元组与逻辑运算符
Oct 13 Python
python实现unicode转中文及转换默认编码的方法
Apr 29 Python
Python线程池模块ThreadPoolExecutor用法分析
Dec 28 Python
python3正则提取字符串里的中文实例
Jan 31 Python
详解用python写网络爬虫-爬取新浪微博评论
May 10 Python
使用Python检测文章抄袭及去重算法原理解析
Jun 14 Python
Python完成哈夫曼树编码过程及原理详解
Jul 29 Python
Python爬虫爬取Bilibili弹幕过程解析
Oct 10 Python
python解包概念及实例
Feb 17 Python
Django 自定义404 500等错误页面的实现
Mar 08 #Python
Python loguru日志库之高效输出控制台日志和日志记录
Mar 07 #Python
Centos7下源码安装Python3 及shell 脚本自动安装Python3的教程
Mar 07 #Python
Django接收照片储存文件的实例代码
Mar 07 #Python
Python实现对adb命令封装
Mar 06 #Python
对Python中 \r, \n, \r\n的彻底理解
Mar 06 #Python
python去除删除数据中\u0000\u0001等unicode字符串的代码
Mar 06 #Python
You might like
造就帕卡马拉的帕卡斯是怎么被发现的
2021/03/03 咖啡文化
深入理解PHP原理之错误抑制与内嵌HTML分析
2011/05/02 PHP
PHP如何实现跨域
2016/05/30 PHP
javascript hasFocus使用实例
2010/06/29 Javascript
javascript结合html5 canvas实现(可调画笔颜色/粗细/橡皮)的涂鸦板
2013/04/27 Javascript
JS实现判断碰撞的方法
2015/02/11 Javascript
Vue.js每天必学之构造器与生命周期
2016/09/05 Javascript
用jQuery实现优酷首页轮播图
2017/01/09 Javascript
微信小程序商品详情页规格属性选择示例代码
2017/10/30 Javascript
Vue全家桶实践项目总结(推荐)
2017/11/04 Javascript
详解Angular系列之变化检测(Change Detection)
2018/02/26 Javascript
解决淘宝cnpm 安装后cnpm不是内部或外部命令的问题
2018/05/17 Javascript
浅谈super-vuex使用体验
2018/06/25 Javascript
图解NodeJS实现登录注册功能
2019/09/16 NodeJs
[35:29]Secret vs VG 2018国际邀请赛淘汰赛BO3 第三场 8.23
2018/08/24 DOTA
[01:10:48]完美世界DOTA2联赛PWL S2 GXR vs PXG 第一场 11.18
2020/11/18 DOTA
python正则表达式抓取成语网站
2013/11/20 Python
解决python写的windows服务不能启动的问题
2014/04/15 Python
用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
2015/11/05 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
Python实现删除排序数组中重复项的两种方法示例
2019/01/31 Python
pytorch实现mnist数据集的图像可视化及保存
2020/01/14 Python
Python将list元素转存为CSV文件的实现
2020/11/16 Python
Python3.9最新版下载与安装图文教程详解(Windows系统为例)
2020/11/28 Python
详解BeautifulSoup获取特定标签下内容的方法
2020/12/07 Python
HTML5新增的表单元素和属性实例解析
2014/07/07 HTML / CSS
LN-CC美国:伦敦时尚生活的缩影
2019/02/19 全球购物
原材料检验岗位职责
2014/03/15 职场文书
国贸专业求职信
2014/06/28 职场文书
学习实践科学发展观心得体会
2014/09/10 职场文书
大三学生学年自我鉴定
2014/09/12 职场文书
2014班子成员自我剖析材料思想汇报
2014/10/01 职场文书
暑假社会实践证明格式
2014/10/28 职场文书
写作指导:怎么书写竞聘演讲稿?
2019/07/04 职场文书
用python开发一款操作MySQL的小工具
2021/05/12 Python
Windows server 2012搭建FTP服务器
2022/04/29 Servers