【问题标题】:Templates directory outside Django rootDjango根目录外的模板目录
【发布时间】:2021-09-09 15:50:53
【问题描述】:

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

我试图让 Django 使用 get_templates('foo.html') 命令访问 /var/opt/backoffice/templates 中的 foo.html 模板,但无论我尝试什么,我都会收到错误:django.html。 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 中的模板?

谢谢。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

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

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

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

    【讨论】:

    • 爱德华多,你正式成为我的英雄!祝朋友周末愉快!
    • 我不敢相信我犯了这么愚蠢的错误。
    • 它发生了 :P 很高兴我能帮上忙 :)
    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 2012-07-18
    • 2015-06-25
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多