django.template.exceptions.TemplateDoesNotExist: 500.html

10月前 ⋅ 336 阅读

使用django框架编写自定义错误页面时出现找不到模板错误django.template.exceptions.TemplateDoesNotExist: 500.html。具体情况如下:
1.具体报错如下:

Traceback (most recent call last):
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\core\handlers\wsgi.py", line 124, in __call__
    response = self.get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\core\handlers\base.py", line 140, in get_response
    response = self._middleware_chain(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\core\handlers\exception.py", line 57, in inner
    response = response_for_exception(request, exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\core\handlers\exception.py", line 140, in response_for_exception
    response = handle_uncaught_exception(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\core\handlers\exception.py", line 185, in handle_uncaught_exception
    return callback(request)
           ^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\PycharmProjects\Django\django5\StatusView\views.py", line 36, in error
    return  render(request,'500.html')
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\shortcuts.py", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\anaconda3\envs\python3_11\Lib\site-packages\django\template\loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: 500.html
[28/Jun/2024 16:52:04] "GET / HTTP/1.1" 500 59

2.分析:重点是django.template.exceptions.TemplateDoesNotExist: 500.html这个提示。该提示主要说的是找不到500.html模板。但是我这里明明将500.html写在了templates文件夹下,说明该文件的读取路径不正确。 我的项目文件布局如下:

django5
----django5
-------asgi.py
-------settings.py
-------urls.py
-------wsgi.py
-------_init_.py
----templates
-------500.html
-------400.html
----其他文件或者文件夹

3.在网上查找了一番,最后发现本项目里面的没有对模板路径进行合理的配置,于是在settings.py文件里面找到TEMPLATES配置。对里面的DIRS配置进行修改,将

"DIRS": []

修改为

"DIRS": [os.path.join(BASE_DIR, 'templates')]

4.再次运行项目,发现django就能找到500.html文件了。 注意:如果还出现问题,可能是500.html写成了/500.html或者xxxx/500.html这种方式,导致路径找不到。请仔细查看并试着修改下。