UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

created at 07-17-2021 views: 2

Background: django == 2.2

Traceback (most recent call last):
  File "D:\software\python3.6\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "D:\software\python3.6\lib\site-packages\django\contrib\staticfiles\handlers.py", line 65, in __call__
    return self.application(environ, start_response)
  File "D:\software\python3.6\lib\site-packages\django\core\handlers\wsgi.py", line 141, in __call__
    response = self.get_response(request)
  File "D:\software\python3.6\lib\site-packages\django\core\handlers\base.py", line 75, in get_response
    response = self._middleware_chain(request)
  File "D:\software\python3.6\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "D:\software\python3.6\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:\software\python3.6\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "D:\software\python3.6\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "D:\software\python3.6\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

Solution

python3.6\Lib\site-packages\django\views\debug.py

original:

def get_traceback_html(self):
    """Return HTML version of debug 500 HTTP error page."""
    with Path(CURRENT_DIR, 'templates', 'technical_500.html').open() as fh:
        t = DEBUG_ENGINE.from_string(fh.read())
    c = Context(self.get_traceback_data(), use_l10n=False)
    return t.render(c)

change to:

def get_traceback_html(self):
    """Return HTML version of debug 500 HTTP error page."""
    with Path(CURRENT_DIR, 'templates', 'technical_500.html').open(encoding="utf-8") as fh:
        t = DEBUG_ENGINE.from_string(fh.read())
    c = Context(self.get_traceback_data(), use_l10n=False)
    return t.render(c)
Please log in to leave a comment.