Django自定义500的错误显示信息
这个方法应该对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的错误显示信息的更多相关文章
- 造成win2008 500内部错误不能显示详细错误的原因和解决方法
造成500错误常见原因有:ASP语法出错.ACCESS数据库连接语句出错.文件引用与包含路径出错.使用了服务器不支持的组件如FSO等. 另外,对于win2008的IIS默认不显示详细出错信息的问题以下 ...
- form(form基础、标签渲染、错误显示 重置信息、form属性、局部钩子、全局钩子)
form基础 Django中的Form使用时一般有两种功能: 1.生成html标签 2.验证输入内容 要想使用django提供的form,要在views里导入form模块 from django im ...
- Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php
Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...
- nginx自定义500,502,504错误页面无法跳转【转】
1.自定一个页面,这个页面是一个链接地址可以直接访问的. 以下是nginx的配置: location / { proxy_pass http://tomcat_app108; ...
- 微信通过openID发送消息/后台post、get提交并接收数据 C# .NET 配置404,500等错误信息 连接字符串
微信通过openID发送消息/后台post.get提交并接收数据 控制器:下面是post发送消息(微信不支持从前台发送数据,之前试过,报错,需要跨域,跨域的问题解决后还不行,最后发现之后后端提交 ...
- Laravel自定义错误提示,自定义异常类提示,自定义错误返回信息,自定义错误页面
方法一 新增CustomException.php文件 App\Exceptions\CustomException.php <?php namespace App\Exceptions; us ...
- Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php
Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...
- C# .NET 配置404,500等错误信息
<customErrors mode="On" defaultRedirect="viewAll.html"><!--所有的错误显示页--&g ...
- django 自定义标签和过滤器
django 自定义标签和过滤器 Django支持自定义标签和过滤器.起初还不太重视它这项功能,但最近试了试自定义标签.发现django这个功能实在是太爽了. 首先在你项目的一个app中建立一个pyt ...
随机推荐
- POJ3255
题目链接:http://poj.org/problem?id=3255 解题思路: 昨晚两点多睡不着翻起来刷<挑战>的题,结果遇到这道求次短路的题,一脸懵逼.想了半小时没什么思路就看他的解 ...
- Lodash工具库中cloneDeep深拷贝的使用
在vue向数据库提交数据的过程中,可能需要处理某些数据,比如有以下要求:传递的参数必须是以逗号分隔的分类列表 但此时如果vue组件中的数据却是数组形式的这个时候需要用到数组的 join 方法对数据进行 ...
- ajax 请求PHP返回json格式的处理
php返回代码格式 public function json(){ if (request()->isAjax()){ $data = [ 'code'=>'1', 'msg'=>' ...
- 通过swagger json一键解析为html页面、导出word和excel的解析算法分享
写在前面: 完全通过Spring Boot工程 Java代码,将swagger json 一键解析为html页面.导出word和execel的解析算法,不需要任何网上那些类似于“SwaggerMark ...
- 这次终于可以愉快的进行 appium 自动化测试了
appium 是进行 app 自动化测试非常成熟的一套框架.但是因为 appium 设计到的安装内容比较多,很多同学入门都跪在了环境安装的部分.本篇讲述 appium 安卓环境的搭建,希望让更多童鞋轻 ...
- Win10企业版远程桌面结合frp实现公网远程
Win10企业版远程桌面结合frp实现公网远程 前言 由于经常下班后还要处理问题,但是又没有运维那么频繁,就不想天天背着电脑来回跑,刚开始用的teamviewer,后来被商业劝退了(就是不让用了,让买 ...
- [优文翻译]003.你应避免的移动开发APP的5个细节(5 Things to Avoid while Developing Your Next Mobile App)
导读:本文是从<5 Things to Avoid while Developing Your Next Mobile App>这篇文章翻译而来 智能手机的普及带动了大批移动应用的诞生,这 ...
- 【RT-Thread笔记】OneNet软件包的使用
去年,RT-Thread发布了RT-Thread Studio初版RT-ThreadStudio的使用体验,经过不断更新迭代之后,来到了V1.1.0,咱也来拥抱一下新版本. 本篇笔记咱们以接入OneN ...
- Beta冲刺 —— 6.1
这个作业属于哪个课程 软件工程 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.讨论并解决每个人存在的问 ...
- akka-typed(3) - PersistentActor has EventSourcedBehavior
akka-typed中已经没有PersistentActor了.取而代之的是带有EventSourcedBehavior的actor,也就是一种专门支持EventSource模式的actor.Even ...