[Answered ]-Pointing Django to Different Template Directory

1👍

The following code worked for me. As it turned out, one needed to specify an additional entry in the DIR list of the TEMPLATES list.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),

            #re-route the search for templates into this custom template directory
            os.path.join(os.path.join(BASE_DIR, 'templates'), 'tshirt-theme'),

            #Uncomment the line below to restore the original Oscar template
            #OSCAR_MAIN_TEMPLATE_DIR,
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.contrib.messages.context_processors.messages',

                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.promotions.context_processors.promotions',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.core.context_processors.metadata',
            ],
        },
    },
]

1👍

You could also assign the different path to directories as variables :

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates/local')
OSCAR_TEMP_DIR = os.path.join(BASE_DIR, 'templates/oscar')
TEMP_DIR = os.path.join(BASE_DIR, 'templates')

And in the Templates section of settings.py, assign these variables to Directory arg.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR, OSCAR_TEMP_DIR, TEMP_DIR],
        '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',

                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.core.context_processors.metadata',
            ],
        },
    },
]

Now, you can put and use your templates from either

  1. project_dir/templates/local OR
  2. project_dir/templates/oscarOR
  3. project_dir/templates/ For Any template file that intersects
    both(eg: main base.html).

Just easier to read and cleaner code if you have to use multiple template directories.

Leave a comment