Python Django:TemplateDoesNotExist位于//
我试图呈现一个简单的页面,但遇到了一个问题Python Django:TemplateDoesNotExist位于//,python,django,Python,Django,我试图呈现一个简单的页面,但遇到了一个问题 TemplateDoesNotExist at /pages/ {} 找不到模板文件夹。这是我的设置.py的配置 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates',
TemplateDoesNotExist at /pages/
{}
找不到模板文件夹。这是我的设置.py的配置
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'pages/template')],
'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',
],
},
},
]
from django.shortcuts import render
# Create your views here.
def index(request):
context = {}
return render(request, 'index.html', context)
我做错什么了吗
---------------指令----------------
-----------更新-------------
文件树
视图.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'pages/template')],
'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',
],
},
},
]
from django.shortcuts import render
# Create your views here.
def index(request):
context = {}
return render(request, 'index.html', context)
调用
render
时,您忘记了第一个参数request
return render(request, 'index.html', context)
您可以有多个模板目录,例如
src/template
和pages/template
。如果您想要一个src/template
目录,那么您需要将其包含在DIRS
选项中
'DIRS': [os.path.join(BASE_DIR, 'templates')],
你不需要
目录中的页面/模板
——应用程序加载器将找到该目录中的模板,因为你将应用程序目录
设置为真
,并且页面
在你的安装的应用程序
设置中。你的模板路径错误。
默认情况下,django拥有html文件的文件夹“template”,请尝试在应用程序文件夹中创建名为template的文件夹,并在其中创建文件index.html我犯了以下错误:
TemplateDoesNotExist(template_name,chain=chain)django.template.exceptions.TemplateDoesNotExist:index.html
我曾经通过PyCharm创建虚拟环境,因为它没有安装从模板文件夹加载模板所需的所有工具
现在,我通过命令提示符创建我的命令,并安装Django和我需要的任何其他与pip install“something”相关的东西
注意:
我知道这不能回答问题,但这就是我做错的。我偶然发现了这个问题,其他人也可能犯了同样的错误。您可能忘了在“已安装的应用程序”设置中添加应用程序
已安装的应用程序=[
...
]请检查settings.py文件中是否声明了正确的应用程序名称,并检查
TEMPLATE_DIR=os.path.join(BASE_DIR,'templatefoldername') and TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'APP_DIRS': True,
是正确的。请同时签入views.py中提到的html文件名是正确的。(返回render(请求,'details/htmlfile',context=my_dict)
)您只需要提到html文件所在文件夹的名称
'DIRS':[os.path.join(基本目录,'templates')]
回到view.py文件,只需按如下方式保存它
return render(request, 'file_name.html')
-- run the server
请发布您的项目目录结构。@Saturnix更新了它。请将您的模板文件夹移到“页面”之外,并称之为“模板”。输入您的设置:TEMPLATE\u DIRS=(os.path.join(BASE\u DIR,'templates'),)
。现在,在your views.py中,使用render('index.html',context)
@Saturnix将模板重命名为templates,将其移动到src子文件夹中,修改设置,但仍然收到相同的错误消息。(我更新了views.py文件,以防万一)如果需要,您可以有多个模板文件夹。请参阅我的最新答案。