【发布时间】:2021-09-30 01:33:41
【问题描述】:
我对 django 很陌生,我正在尝试使用项目级模板,因为我的项目应该包含多个单一的应用程序。为了做到这一点,我创建了一个layout.html 文件,我的所有模板都应该从中扩展。这是我的文件结构:
├───Project
│ ├───templates
│ └───__pycache__
├───Courses
│ ├───migrations
│ ├───templates
│ │ └───Courses
│ └───__pycache__
└───PyCourse
├───migrations
│ └───__pycache__
├───static
│ └───PyCourse
├───templates
│ └───PyCourse
└───__pycache__
通过尝试记录如何使用项目级模板,我发现我们必须指定引擎在哪里查找模板。这就是我在我的settings.py 文件中要做的事情:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
},
},
]
然后,我认为只需像这样从项目布局中扩展我的模板:{% extends 'layout.html' %} 就可以了,但我收到以下错误:
django.template.exceptions.TemplateDoesNotExist: layout.html
我看到论坛上有人问了一些类似的问题,但尝试实施答案对我来说并没有真正奏效。可能是因为这些问题是多年前提出的。
无论如何,我真的很感谢那些能够帮助我处理这个问题的人。它已经为此苦苦挣扎了一段时间XD
----EDIT1----
layout.html 文件位于 templates 文件夹中:
Project
└───templates
└───layout.html
【问题讨论】:
-
layout.html在哪里? -
它位于我的项目文件夹中名为
templates的文件夹中(我们有setting.py、wsgi.py等文件) -
将其移动到您的
Project目录中的templates目录。这是您在TEMPLATES['DIRS']中指定的内容。 -
是的,这就是它的实际位置,抱歉,如果我没有明确说明它
-
你能试试
printBASE_DIR的值吗?