这个方法应该对Django version 2.2.4以后的版本都有效,因为我的项目是这个版本,其他版本我并没有测试。

首先找到Django的exception.py文件路径:  C:\python_37\Lib\site-packages\django\core\handlers\exception.py,要找到自己开发的Python环境的文件夹,我懒得用虚拟环境就直接使用了本地的Python环境。

之前找了很对相关的文章,踩了很多坑,发现Django version 2.2.4,这个版本已经把 handle_uncaught_exception 方法,提出来放到 exception.py里了。

def handle_uncaught_exception(request, resolver, exc_info):
"""
Processing for any otherwise uncaught exceptions (those that will
generate HTTP 500 responses).
"""
# if settings.DEBUG_PROPAGATE_EXCEPTIONS:
# raise # if settings.DEBUG:
# return debug.technical_500_response(request, *exc_info) # # Return an HttpResponse that displays a friendly error message.
# callback, param_dict = resolver.resolve_error_handler(500)
# print(param_dict)
# return callback(request, **param_dict)
"""
Processing for any otherwise uncaught exceptions (those that will
generate HTTP 500 responses). Can be overridden by subclasses who want
customised 500 handling.
Be *very* careful when overriding this because the error could be
caused by anything, so assuming something like the database is always
available would be an error.
"""
from django.conf import settings if settings.DEBUG_PROPAGATE_EXCEPTIONS:
raise
if settings.DEBUG:
from django.views import debug
return debug.technical_500_response(request, *exc_info)
else:
logger = logging.getLogger("logger")
logger.error('Internal Server Error: %s', request.path,
exc_info=exc_info,
extra={
'status_code': 500,
'request': request
}
)
params = request.POST
userid = params.get("userid","") #============= Code start ========== by chang
# print("---------- HTTP 500 Error Msg ---------- ")
# print(exc_info)
# print(type(exc_info))
# print(traceback.format_exc())
# print("---------------------------------------- " )
#
# 在这里自定义自己想要数据,调用 HttpResponse方法返回,这样在DEBUG = True的状态下,在线上的情况中,我们就能看到自己想要看的错误信息,尽量不要这样操作。
import time
errorinfo = str(exc_info[1])
error_url = request.path
error_log = traceback.format_exc()
error_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
error = {"error": errorinfo}
error["code"] = 500
error["url"] = error_url
error["time"] = error_time
error["log"] = error_log
return HttpResponse(json.dumps(error), content_type= 'application/json' , status=500)
#============= Code end =========
# 我已经注释掉了原有Django的方法,我觉得这样不是很友好,只有一个500,大家看起来很难受,但是线上版本还是把这些信息隐藏起来,或者恢复Django的代码。
# # If Http500 handler is not installed, re-raise last exception
# if resolver.urlconf_module is None:
# raise (exc_info[1], None, exc_info[2])
# # Return an HttpResponse that displays a friendly error message.
# callback, param_dict = resolver.resolve500()
# return callback(request, **param_dict)
#
# Return an HttpResponse that displays a friendly error message.
# callback, param_dict = resolver.resolve_error_handler(500)
# return callback(request, **param_dict)

  

Django自定义500的错误显示信息的更多相关文章

  1. 造成win2008 500内部错误不能显示详细错误的原因和解决方法

    造成500错误常见原因有:ASP语法出错.ACCESS数据库连接语句出错.文件引用与包含路径出错.使用了服务器不支持的组件如FSO等. 另外,对于win2008的IIS默认不显示详细出错信息的问题以下 ...

  2. form(form基础、标签渲染、错误显示 重置信息、form属性、局部钩子、全局钩子)

    form基础 Django中的Form使用时一般有两种功能: 1.生成html标签 2.验证输入内容 要想使用django提供的form,要在views里导入form模块 from django im ...

  3. Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php

    Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...

  4. nginx自定义500,502,504错误页面无法跳转【转】

    1.自定一个页面,这个页面是一个链接地址可以直接访问的. 以下是nginx的配置: location / {            proxy_pass http://tomcat_app108;   ...

  5. 微信通过openID发送消息/后台post、get提交并接收数据 C# .NET 配置404,500等错误信息 连接字符串

    微信通过openID发送消息/后台post.get提交并接收数据   控制器:下面是post发送消息(微信不支持从前台发送数据,之前试过,报错,需要跨域,跨域的问题解决后还不行,最后发现之后后端提交 ...

  6. Laravel自定义错误提示,自定义异常类提示,自定义错误返回信息,自定义错误页面

    方法一 新增CustomException.php文件 App\Exceptions\CustomException.php <?php namespace App\Exceptions; us ...

  7. Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php

    Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...

  8. C# .NET 配置404,500等错误信息

    <customErrors mode="On" defaultRedirect="viewAll.html"><!--所有的错误显示页--&g ...

  9. django 自定义标签和过滤器

    django 自定义标签和过滤器 Django支持自定义标签和过滤器.起初还不太重视它这项功能,但最近试了试自定义标签.发现django这个功能实在是太爽了. 首先在你项目的一个app中建立一个pyt ...

随机推荐

  1. os.walk()的实际应用

    背景: 通过Mobaxterm从本地上传虹膜数据,一共79个类,每类里包含左右眼各400张数据,总共63200张,上传期间断网不确定是否传完. 思路: 1.首先遍历总类别数是否正确,若不足79,返回“ ...

  2. PLC可编程控制器的结构和工作原理

    PLC的可编程控制器由的功能结构由cpu中央处理器,存储器和输入输出借口三部分组成 CPU Cpu的功能是完成plc所有的的控制和监视, Cpu中央处理去由控制器,寄存器,运算器.通过数据总线,地址总 ...

  3. .Net Core之仓储(Repository)模式

    我们经常在项目中使用仓储(Repository)模式,来实现解耦数据访问层与业务层.那在.net core使用EF core又是怎么做的呢? 现在我分享一下我的实现方案: 一.在领域层创建Reposi ...

  4. Redis详解(十)------ 从零开始搭建集群

    在上一篇博客我们介绍了------Redis哨兵(Sentinel)模式,哨兵模式主要是解决高可用问题,在master节点宕机时,slave节点能够自动切换成为master节点 本篇博客我们来介绍Re ...

  5. 配置单机Kafka

    配置单机kafka 关闭selinux,开启防火墙9092端口 1.关闭selinux vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXT ...

  6. [COCOS2DX-LUA]0-004.cocos2dx中的DrawNode的init的方法问题

    1.诱因 近期,项目接入了Bugly, 上报了一些平常测试不出来,或者很难重现的bug,这类bug非常难排查.原因有二,第一,问题无法重现,第二,修改了无法立即验证结果.有一个问题困恼了我很久,就是一 ...

  7. golang内置类型和内置函数

    golang内置类型和内置函数是不需要引入包直接可用的 golang内置类型: 数值类型 string int,unint float32,float64 bool array 有长度的 comple ...

  8. Rocket - tilelink - Bits vs. UInt

    https://mp.weixin.qq.com/s/lzDmIHkUph3b1Fxgx66ySg   分析移位/取反操作在Intellij中提示错误的问题.   1. 问题   用到移位/取反的地方 ...

  9. Rocket - diplomacy - LazyModule的实例化

    https://mp.weixin.qq.com/s/9PsBt4_4qHx4i6C5XtuiUw   介绍LazyModule和Node构造方法的执行过程,即实例化过程.     1. NullIn ...

  10. 使用Mac的Remote Desktop Manager连接ubuntu16.04 & Win10的远程桌面

    疫情严重,公司实行远程办公.自己只有mac电脑,苦于3个系统间跨平台建立远程桌面. 今天,终于尝试成功!特来记录,以防别人踩坑! Mac远程软件安装 Remote Desktop Manager软件非 ...