我的 Django 项目中有以下 View :
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
实际存在:
我不知道我是否缺少任何 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',
],
},
},
]
最佳答案
首先,您应该始终将 html 文件和静态文件(css、js、json)分开。
这对我来说是一个很好的方法,我一直使用。对于 HTML 文件,在项目文件夹中定义模板目录。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
.
.
.
},
]
此templates
文件夹应位于项目目录中。在此文件夹中,您可以为您的应用程序或任何内容创建文件夹。就像当前的情况一样,templates
中有一个 public
文件夹,里面有 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')
但是静态文件需要单独的文件夹。
关于python - Django渲染无法找到实际存在的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58108983/