如何使用 Django 路径来定位和加载模板?姜戈 3.2

问题描述 投票:0回答:4

在 django3.2 中我试图用它来定位和加载模板? 但对我不起作用

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ` [BASE_DIR / 'templates']`,
    }

默认设置如下:

`from pathlib import Path`

# Build paths inside the project like this: BASE_DIR / 'subdir'.

BASE_DIR = Path(__file__).resolve().parent.parent 

有可能出现什么问题吗?

python django templates helper django-3.2
4个回答
0
投票

尝试使用

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
    }

0
投票

如果你想改变你的模板路径,你可以在你的settings.py中使用它,例如我在项目的根目录中有一个用于我的模板的“templates”目录:

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
    },
]

0
投票

在 Django 中尝试此编辑

settings.py

TEMPLATES_DIR=BASE_DIR / 'templates'(line-17)
'DIRS':[TEMPLATES_DIR,],(line-58)

0
投票

如果您的设置位置位于类似目录下

~/proj/proj/settings/local.py

那么 BASE_DIR 应该多一个

.parent

那么您的 DIRS 设置将不需要任何修复。

© www.soinside.com 2019 - 2024. All rights reserved.