Django 在使用模块时出现编码错误的解决方法

最近在学习 Django 在使用模块时遇到了编码错误

进入页面会显示

A server error occurred. Please contact the administrator.

解决的方法是在虚拟环境目录下,如: venv\Lib\site-packages\django\views 找到 debug.py 将第331行代码修改为:

with Path(CURRENT_DIR, 'templates', 'technical_500.html').open(encoding='utf-8') as fh:

错误时 traceback 如下:

Traceback (most recent call last):
  File "项目路径\ll_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "项目路径\ll_env\lib\site-packages\django\utils\deprecation.py", line 94, in __call__
    response = response or self.get_response(request)
  File "项目路径\ll_env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "项目路径\ll_env\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 "项目路径\ll_env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "项目路径\ll_env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "项目路径\ll_env\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