Django 根目录外的模板目录

时间:2021-06-26 17:22:44

标签: django django-templates

我有一个模板目录 /var/opt/backoffice/templates,里面有一个名为 foo.html 的模板。该目录位于 Django 根目录之外,即 /opt/backoffice。

我试图让 Django 使用 get_templates('foo.html') 命令访问 /var/opt/backoffice/templates 中的 foo.html 模板,但无论我尝试什么,我都会收到错误:django。 template.exceptions.TemplateDoesNotExist: foo.html

我的模板 settings.py 是:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
           
            'DIRS': [
            'var/opt/backoffice/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',
                ],
            },
        },
    ]

我的问题是,如何配置 Django 以便我可以使用 get_templates('foo.html') 访问 /var/opt/backoffice/templates 中的模板?

谢谢。

1 个答案:

答案 0 :(得分:1)

您在 var 之前缺少一个斜杠,这使它成为子目录而不是根级目录:

'DIRS': [
    '/var/opt/backoffice/templates'
],

我没有检查 /var,但它在 Django 项目之外的主目录的另一个文件夹中对我有用

相关问题