Django UnicodeDecodeError 在错误报告期间

Django UnicodeDecodeError during error reporting

提问人:maxbellec 提问时间:5/24/2019 最后编辑:maxbellec 更新时间:5/27/2019 访问量:633

问:

错误报告不断崩溃。在回溯中,我首先得到实际错误,然后很多倍相同

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/www/exc2-backend/.venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/var/www/exc2-backend/.venv/lib/python3.6/site-packages/django/utils/deprecation.py", line 94, in __call__
    response = response or self.get_response(request)
  File "/var/www/exc2-backend/.venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "/var/www/exc2-backend/.venv/lib/python3.6/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 "/var/www/exc2-backend/.venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/var/www/exc2-backend/.venv/lib/python3.6/site-packages/django/views/debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "/var/www/exc2-backend/.venv/lib/python3.6/site-packages/django/views/debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
  File "/var/www/exc2-backend/.venv/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9735: ordinal not in range(128)

我正在使用 和 .我不明白这是从哪里来的。Python3.6Django==2.2UnicodeDecodeError

在生产服务器上运行

from django.test.client import RequestFactory
import sys
from django.views.debug import ExceptionReporter

request = RequestFactory().get('/dummy')
exc_info = sys.exc_info()
reporter = ExceptionReporter(request, is_email=True, *exc_info)
reporter.get_traceback_html()

工作而不会崩溃。

我正在使用以下命令使用 gunicorn 运行我的网站

/var/www/exc2-backend/.venv/bin/gunicorn exc2_backend.wsgi --name "exc2_backend" --workers=4 --bind=0.0.0.0:8000 --user="www-data" --group="www-data"

我缩小了问题范围,这发生在这里

from django.views.debug import ExceptionReporter, CURRENT_DIR, DEBUG_ENGINE
from pathlib import Path

with Path(CURRENT_DIR, 'templates', 'technical_500.html').open() as fh:
    data = fh.read()

如果我更改为 ,则没有错误。Path(...).open(encoding='utf8')

该错误不会发生在 shell 上,只有在使用 gunicorn 运行代码时才会发生。

在服务器上,即使通过 gunicorn,返回 ,但返回 !sys.getdefaultencoding()utf-8locale.getpreferredencoding(False)ANSI_X3.4-1968

有什么想法吗?

python django 错误报告

评论

0赞 maxbellec 5/24/2019
每当使用错误报告时,都会发生这种情况。这样我就不会收到报告错误的电子邮件。我只能通过查看服务器上的日志来查看错误。该网站仍在运行,我或多或少会看到错误,但我宁愿通过电子邮件获取它们,因为它通常是这样完成的。
0赞 snakecharmerb 5/25/2019
gunicorn 是否在其他用户下运行?如果是这样,他们的是什么?locale
0赞 maxbellec 5/26/2019
是的,作为 ,但具有相同的区域设置www-data
0赞 snakecharmerb 5/26/2019
区域设置是什么?
0赞 maxbellec 5/27/2019
en_US.UTF-8

答:

0赞 maxbellec 5/27/2019 #1

我在 Gunicorn () 中设置的区域设置未安装在系统上,因此不知何故 Python 默认为编码。fr_FR.UTF8ANSI_X3.4-1968

解决方案是安装缺少的区域设置:sudo locale-gen fr_FR.UTF-8