【问题标题】:I'm trying to create a Web App using Django, I've encountered unknown 500 errors I can't debug我正在尝试使用 Django 创建一个 Web 应用程序,我遇到了无法调试的未知 500 错误
【发布时间】:2020-07-25 20:40:32
【问题描述】:

这是发生的错误,当我从我的模板文件夹中请求一个 .html 文件时似乎会发生这种错误,但由于某些未知的原因,它无法显示并抛出我无法处理的这些错误弄清楚几天。

如果需要更多信息,我会提供给任何提供帮助的人

Traceback (most recent call last):
  File "C:\Users\uknow\Desktop\Coding\GoFishFarms\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\uknow\Desktop\Coding\GoFishFarms\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\uknow\Desktop\Coding\GoFishFarms\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\uknow\Desktop\Coding\GoFishFarms\pages\views.py", line 5, in index
    return render(request, 'index.html')
  File "C:\Users\uknow\Desktop\Coding\GoFishFarms\venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\uknow\Desktop\Coding\GoFishFarms\venv\lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\Users\uknow\Desktop\Coding\GoFishFarms\venv\lib\site-packages\django\template\loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: index.html
[25/Jul/2020 13:29:40] "GET / HTTP/1.1" 500 82139

Views.py

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'index.html')

def about(request):
    return render(request, 'about.html')

Directory Structure

【问题讨论】:

  • 你能分享你的views.py 和模板的目录结构吗?
  • 你去我添加了views.py和目录截图

标签: django django-errors


【解决方案1】:

您正确设置了目录结构(最佳实践),现在当 Django“设置”主模板文件夹时,它看起来像这样 templates/, templates/pages/index.html, templates/pages/about .htmlrender() 将自动获取 templates/ 部分,因此您必须将其余部分交给它 - render(request, 'pages/index.html')

重要 记得在 settings.py 中将 pages 应用添加到 INSTALLED_APPS

INSTALLED_APPS = [
...
'pages',
...
]

否则 Django 不会将 pages 附加到您的模板文件夹中

【讨论】:

  • 我已经安装了 pages 应用程序,我修复了 views.py,正如你所说的,我仍然遇到同样的错误,无法修复。我将放弃 Django 和 Python,看看是否有另一种语言更适合我的后端。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 2016-05-22
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多