【问题标题】:using project-level templates django使用项目级模板 django
【发布时间】: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.pywsgi.py 等文件)
  • 将其移动到您的Project 目录中的templates 目录。这是您在TEMPLATES['DIRS'] 中指定的内容。
  • 是的,这就是它的实际位置,抱歉,如果我没有明确说明它
  • 你能试试printBASE_DIR的值吗?

标签: django django-templates


【解决方案1】:

我一直在努力解决同样的问题,我已经设法解决了它,所以希望我的解决方案对你有用。我正在使用 Django 版本 3.2.5

背景

大多数其他类似问题的答案都显示了 settings.py 文件使用的解决方案

...
    DIRS = [
        os.path.join(BASE_DIR, 'templates')
]

我认为这是产生路径的旧方法。相反,在 settings.py 文件的开头,您会看到

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

所以我的建议是使用这个约定来生成模板路径。

解决方案

我犯的错误是假设 BASE_DIR 指的是项目子文件夹。看起来你已经做了同样的事情。所以在你上面的例子中,我会改变这个:

TEMPLATES = [
...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
etc..
]

到这里:

TEMPLATES = [
...
        'DIRS': [BASE_DIR / 'Project/templates')],
        'APP_DIRS': True,
etc..
]

我认为django-admin startproject 命令生成文件夹(命名空间)时会出现混淆。因为它在同名的项目文件夹“Project”中放置了一个子文件夹“Project”,所以很容易将BASE_DIR变量误认为是子文件夹。但事实并非如此。

Project ##This is your BASE_DIR
├───Project ##This is not your BASE_DIR
│   ├───templates ##This is where you're trying to target
│   ├───asgi.py
│   ├───__init__.py
│   ├───settings.py
│   ├───urls.py
│   ├───wsgi.py
│   └───__pycache__
├───app1
│   ├───migrations
│   ├───templates
│   │   └───app1
│   └───__pycache__
└───manage.py

【讨论】:

  • 当然,如果您想使用该方法,只需将“项目”添加到[os.path.join(BASE_DIR, 'templates')][os.path.join(BASE_DIR, 'Project/templates')]
  • 哦,谢谢你,你让我一次又一次地避免复制 html 代码。非常感谢!
猜你喜欢
  • 2013-06-13
  • 2017-03-12
  • 2013-01-21
  • 2014-05-20
  • 2014-01-02
  • 2015-12-01
  • 2018-03-20
  • 2017-02-02
相关资源
最近更新 更多