为什么' DIRS'仅在APP_DIRS为False时有效?

时间:2017-08-10 07:28:02

标签: django

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

我尝试加载自定义html小部件,但更改了' DIRS'没有改变模板加载器事后调查'什么时候' TemplateDoesNotExist'被扔了。当我设置' APP_DIRS'假,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',
        ],
    },
},
]

给出这个输出:

模板加载器postmortem

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',
        ],
    },
},
]

给出了这个输出:

模板加载器postmortem

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)

如果我理解了引擎django'正确地说,示例2的最后一行(' django.template.loaders.filesystem.Loader:/ Users / jonas / Documents / jobb / dynamicSurvey / survey / templates / django / forms / widgets / survey /'如果DIRS设置有效,则应该在示例1中查找。

我是Stack的新手,所以请随意批评我的问题。

1 个答案:

答案 0 :(得分:1)

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

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

正如文档所示,我会再次将'APP_DIRS'设置为True,并将django.forms添加到INSTALLED_APPS,以便Django可以找到默认模板。