James Franco
James Franco

Reputation: 4706

Specify a path other than templates folder for placement of templates

I understand that by default the app looks into the template folder of the apps for a specific template name. What happens if I place the templates in a folder called foo ? How can i tell django to also look under the foo of each folder of every application ?.

Upvotes: 2

Views: 515

Answers (1)

Termi
Termi

Reputation: 661

You can add list of directories to DIRS in TEMPLATES option in django settings

To detect 'foo' template directory under every app, i suggest you to separate the INSTALLED_APPS tuple into minimum two different tuples

  1. 'PROJECT_APPS' = tuple containing names of your apps
  2. 'OTHER_APPS' = tuple of default django apps + third party apps

So INSTALLED_APPS = PROJECT_APPS + OTHER_APPS

Now include the template directories to DIRS in TEMPLATES

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(PROJECT_PATH, 'apps', app_name, 'foo') for app_name in PROJECT_APPS],
    'OPTIONS': {
        'loaders': [
            'django.template.loaders.filesystem.Loader',
            'django.template.loaders.app_directories.Loader',
        ],
        'context_processors': [
            "django.template.context_processors.debug",
            "django.template.context_processors.media",
            "django.template.context_processors.request",
            "django.template.context_processors.static",
        ]
    }
},
]

So now 'django.template.loaders.filesystem.Loader' will look into all the directories in DIRS and by default the 'django.template.loaders.app_directories.Loader' loader will look for 'templates' folder in each app.

Ref: Django loader types

Upvotes: 1

Related Questions