如何使用path Django查找和加载模板?Django 3.2

2024-05-01 21:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

在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 

有什么问题的线索吗


Tags: pathdjangofrom定位模板backendbasedir
2条回答

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

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

试用

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

相关问题 更多 >