django2.2提示编码出错的解决办法
最新的django2.2版本,操作模板的时候,有可能会提示编码出错。 错误代码:
Traceback (most recent call last): File "D:\python\lib\wsgiref\handlers.py", line 137, in run self.result = application(self.environ, self.start_response) File "D:\python\lib\site-packages\django\contrib\staticfiles\handlers.py", line 65, in __call__ return self.application(environ, start_response) File "D:\python\lib\site-packages\django\core\handlers\wsgi.py", line 141, in __call__ response = self.get_response(request) File "D:\python\lib\site-packages\django\core\handlers\base.py", line 75, in get_response response = self._middleware_chain(request) File "D:\python\lib\site-packages\django\core\handlers\exception.py", line 36, in inner response = response_for_exception(request, exc) File "D:\python\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info()) File "D:\python\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception return debug.technical_500_response(request, *exc_info) File "D:\python\lib\site-packages\django\views\debug.py", line 94, in technical_500_response html = reporter.get_traceback_html() File "D:\python\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html t = DEBUG_ENGINE.from_string(fh.read()) UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence
页面显示:
A server error occurred. Please contact the administrator.
解决办法: 在虚拟环境里这个目录下:venv\Lib\site-packages\django\views* 找到debug.py这个文件 如果没有使用虚拟环境,该文件在Python安装目录里的Lib\site-packages\django\views\里 找到该文件的 331行代码如下:
with Path(CURRENT_DIR, 'templates', 'technical_500.html').open() as fh:
修改成:
with Path(CURRENT_DIR, 'templates', 'technical_500.html').open(encoding='utf-8') as fh:
评论 1
十分有效