django主动抛出403异常的方法详解
作者:obaby 时间:2023-03-28 17:40:10
前言
网上的做法基本都是下面的代码
return HttpResponseForbidden()
试了一下,效果一般,没有异常页面显示,最终显示的是浏览器的异常页面,如下图:
设置方法如下:
如果要想让服务器截获异常并且显示错误页可以用下面的方式:
id = request.GET.get('id', '')
timestamp = request.GET.get('timestamp', '')
accesskey = request.GET.get('accesskey', '')
if timestamp == '' or accesskey == '' or id == '':
raise PermissionDenied
此时访问就会显示正常的错误页了,如下
django 错误页面定义方法:
urls.py加入如下代码:
handler403 = permission_denied
handler404 = page_not_found
handler500 = page_error
相关view代码:
# Create your views here.
def page_not_found(request):
return HttpResponseRedirect('/static/error_pages/404.html')
def page_error(request):
return HttpResponseRedirect('/static/error_pages/500.html')
def permission_denied(request):
return HttpResponseRedirect('/static/error_pages/403.html')
来源:http://www.h4ck.org.cn/2018/12/django-主动抛出-403-异常/
标签:django,403,异常
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈一下基于Pytorch的可视化工具
2022-12-28 23:08:07
![](https://img.aspxhome.com/file/2023/2/128452_0s.png)
原生js拖拽实现图形伸缩效果
2024-04-16 08:55:27
![](https://img.aspxhome.com/file/2023/9/136819_0s.jpg)
如何利用Golang解析读取Mysql备份文件
2024-01-28 20:51:19
Mysql如何实现不存在则插入,存在则更新
2024-01-26 11:13:54
![](https://img.aspxhome.com/file/2023/3/118473_0s.png)
Oracle导出excel数据
2023-07-18 17:44:14
如何计算多个订单的核销金额
2024-01-26 17:56:31
![](https://img.aspxhome.com/file/2023/0/104140_0s.png)
对Python3 序列解包详解
2023-06-15 10:39:25
Django打印出在数据库中执行的语句问题
2024-01-21 04:26:41
go redis之redigo的使用
2024-05-22 17:46:37
javascript同页面多次调用弹出层具体实例代码
2024-04-10 14:02:31
使用pycharm将自己项目代码上传github(小白教程)
2022-05-02 20:37:06
![](https://img.aspxhome.com/file/2023/6/82526_0s.jpg)
如何用Python获取计算机名,ip地址,mac地址
2021-12-31 05:41:04
![](https://img.aspxhome.com/file/2023/1/120871_0s.png)
Python机器学习算法之k均值聚类(k-means)
2023-01-17 17:42:14
![](https://img.aspxhome.com/file/2023/9/101259_0s.jpg)
Python正确调用 jar 包加密得到加密值的操作方法
2021-02-26 12:12:19
![](https://img.aspxhome.com/file/2023/3/100913_0s.png)
Python切片操作实例分析
2022-05-02 17:49:25
python合并同类型excel表格的方法
2022-05-08 03:51:52
WEB2.0网页制作标准教程(8)CSS布局入门
2007-09-11 13:21:00
![](https://img.aspxhome.com/file/uploadpic/20079/11/200791113246711.gif)
详解Python 实现元胞自动机中的生命游戏(Game of life)
2023-05-31 07:11:54
![](https://img.aspxhome.com/file/2023/2/120992_0s.png)
网页打开速度的心理学
2009-03-26 13:18:00
ML神器:sklearn的快速使用及入门
2023-04-17 04:42:09
![](https://img.aspxhome.com/file/2023/8/101708_0s.jpg)