Jonas
Jonas

Reputation: 43

Why does 'DIRS' only have an effect when APP_DIRS is False?

Why does 'DIRS' in TEMPLATES in settings.py only have an effect when 'APP_DIRS' is set to False?

I tried to load a custom html widget, but changing the 'DIRS' have not changed the 'Template-loader postmortem' when 'TemplateDoesNotExist' is thrown. When I set the 'APP_DIRS' to False, the 'DIRS' setting suddenly had an effect.

I've tried to search for similar questions, but haven't found an answer. I've also looked through the docs, but neither the paragraph about DIRS nor APP_DIRS mention one working when the other doesn't.

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

Gives this output:

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine 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)

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

gives this output:

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine 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)

If I understand the 'engine django' correctly, the last line of example 2 ('django.template.loaders.filesystem.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/widgets/survey/') should alse be looked for in example 1, if the DIRS setting had an effect.

I'm new to Stack, so feel free to criticize my question.

Upvotes: 4

Views: 2440

Answers (1)

Alasdair
Alasdair

Reputation: 308849

Change your FORM_RENDERER setting to use the TemplatesSetting renderer. It will use the configuration from your TEMPLATES setting.

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

As the docs suggest, I would set 'APP_DIRS' to True again, and add django.forms to your INSTALLED_APPS so that Django can find the default templates.

Upvotes: 3

Related Questions