Stanislav Matšel
Stanislav Matšel

Reputation: 348

Django project template loader setup

Is it possible to set the django project template loading priority so that first of all it loads the apps' "templates" folder and after that the project "templates". If the template exists in the app folder, then use it. If it does not exist in the app folder, then try load from project folder.

Or it is not normal way to load templates? I ask because I see in the exception, that Django tries to load first of all global templates:

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:

  • django.template.loaders.filesystem.Loader: D:\myprojects\my-website\src\templates\home.html (Source does not exist)

  • django.template.loaders.app_directories.Loader: C:\User\Python27\lib\site-packages\django\contrib\admin\templates\home.html (Source does not exist)

  • django.template.loaders.app_directories.Loader: C:\User\Python27\lib\site-packages\django\contrib\auth\templates\home.html (Source does not exist)

  • django.template.loaders.app_directories.Loader: D:\myprojects\my-website\src\website\templates\home.html (Source does not exist)

Upvotes: 1

Views: 1365

Answers (1)

Alasdair
Alasdair

Reputation: 308829

Update your TEMPLATES setting, and put the app_directories loader before the filesystem loader.

If you currently have 'APP_DIRS': True, you will have to remove this, and add the loaders option.

For example, you could change your TEMPLATES setting to:

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'OPTIONS': {
        'loaders': [
            'django.template.loaders.app_directories.Loader',
            'django.template.loaders.filesystem.Loader',
        ],
    },
}]

Upvotes: 4

Related Questions