Django render找不到实际存在的模板

2024-10-03 19:19:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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',
            ],
        },
    },
]

Tags: djangoinpyrequestliblinecodetemplate
2条回答

您需要将目录添加到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.

首先,您应该始终保持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是项目目录,那么:

设置.py

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

        },
    ]

视图.py

return render(request, 'index.html')

如果我们在public中创建另一个文件夹,比如folder1,那么

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

但是静态文件需要单独的文件夹。你知道吗

相关问题 更多 >