Django渲染无法找到实际存在的模板

时间:2019-09-26 02:59:14

标签: python django django-templates django-views

我在Django项目中具有以下视图:

import os
from django.shortcuts import render
from JumboDjango import settings

# Create your views here.
def index(request):
    filename = os.path.join(settings.BASE_DIR, 'frontend/source/public/index.html')
    return render(request, filename)

但是,当它被调用时,它会引发以下异常:

Internal Server Error: /
Traceback (most recent call last):
  File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/hugovillalobos/anaconda3/lib/python3.7/contextlib.py", line 74, in inner
    return func(*args, **kwds)
  File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/views.py", line 8, in index
    return render(request, filename)
  File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: /Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source/public/index.html
[26/Sep/2019 02:51:04] "GET / HTTP/1.1" 500 79956

在以下图像中您可以看到/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source/public/index.html实际上存在:

enter image description here

我不知道我是否缺少任何Django配置或其他内容。

编辑

这是我的模板设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

2 个答案:

答案 0 :(得分:1)

首先,您应始终将html文件和静态文件(css,js,json)分开。

这对我来说是很好的选择,我经常使用。对于HTML文件,请在项目文件夹中定义模板目录。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        .
        .
        .

    },
]

templates文件夹应位于项目目录中。在此文件夹中,您可以为应用程序或其他任何内容创建文件夹。与当前情况类似,public内的templates文件夹和其中的html文件。您可以这样访问该文件:

return render(request, 'public/index.html')

---编辑------

对于此问题的条件,如果 public 是我们将拥有所有html文件的文件夹,而 JumboDjango 是项目目录,则:

settings.py

TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'frontend/source/public/')],
            .
            .
            .

        },
    ]

views.py

return render(request, 'index.html')

如果我们在 public 内部创建另一个文件夹,例如 folder1 ,则

return render(request, 'folder1/index.html')

但是需要用于静态文件的单独文件夹。

答案 1 :(得分:0)

您需要在TEMPLATES键下将目录添加到DIRS设置中。

它允许Django模板引擎在该文件夹中查找模板文件。 例如,如果您的模板目录为/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source

因此,您需要按如下所示将其添加到DIRS

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
             "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source"
        ],
        ...
        ...
    },
]

您需要通过提供render方法的相对路径来呈现模板。

import os
from django.shortcuts import render
from JumboDjango import settings

# Create your views here.
def index(request):
    return render(request, "public/index.html")  # Add a relative path to your template folder here.