【问题标题】:Why does 'DIRS' only have an effect when APP_DIRS is False?为什么 'DIRS' 只有在 APP_DIRS 为 False 时才有效?
【发布时间】:2026-01-25 15:50:01
【问题描述】:

为什么 settings.py 中的 TEMPLATES 中的 'DIRS' 只有在 'APP_DIRS' 设置为 False 时才有效?

我尝试加载自定义 html 小部件,但在抛出“TemplateDoesNotExist”时更改“DIRS”并没有更改“模板加载器事后分析”。当我将“APP_DIRS”设置为 False 时,“DIRS”设置突然生效。

我尝试搜索类似的问题,但没有找到答案。我也浏览了文档,但是关于 DIRS 和 APP_DIRS 的段落都没有提到一个工作,而另一个没有。

示例 1:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates'), '/Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/widgets'],
    '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 尝试按以下顺序加载这些模板:

使用引擎 django:

django.template.loaders.filesystem.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/forms/templates/horizontal_select.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/nested_admin/templates/horizontal_select.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/contrib/admin/templates/horizontal_select.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/contrib/auth/templates/horizontal_select.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/horizontal_select.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/tellme/templates/horizontal_select.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/tinymce/templates/horizontal_select.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/forms/templates/horizontal_select.html (Source does not exist)

示例 2:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates'), '/Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/widgets'],
    'APP_DIRS': False,
    '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 尝试按以下顺序加载这些模板:

使用引擎 django:

django.template.loaders.filesystem.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/templates/survey/survey_detail.html (Source does not exist)
django.template.loaders.filesystem.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/widgets/survey/survey_detail.html (Source does not exist)

如果我正确理解“engine django”,示例 2 的最后一行(“django.template.loaders.filesystem.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/如果 DIRS 设置有影响,还应在示例 1 中查找 widgets/survey/')。

我是 Stack 新手,请随时批评我的问题。

【问题讨论】:

  • 这是哪个 Django 版本?
  • 它的 Django 1.11.2

标签: django


【解决方案1】:

更改您的FORM_RENDERER 设置以使用TemplatesSetting 渲染器。它将使用您的 TEMPLATES 设置中的配置。

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

按照文档的建议,我会再次将'APP_DIRS' 设置为True,并将django.forms 添加到您的INSTALLED_APPS,以便Django 可以找到默认模板。

【讨论】:

  • 'django.forms' 已添加到 'INSTALLED_APPS'。我按照你说的设置了 FORM_RENDERER ,但仍然没有效果。 (无论如何,这是默认设置?)我还将TemplatesSetting中提到的'django.__path__[0] +'/forms/templates''的结果添加到'DIRS'。 'APP_DIRS' 也设置为 True。
  • 默认为DjangoTemplates。我建议使用TemplatesSetting(虽然我一开始不小心复制粘贴了django.forms.renderers.DjangoTemplates)。如果您已经将APP_DIRS 设置为True 并在INSTALLED_APPS 中包含django.forms,则不必添加django.__path__[0] + '/forms/templates' 的结果。
最近更新 更多