Incorrect app is used when template names are same

I have the folder structure like this

a 
    templates
       test.html
b 
    templates
       test.html

When I use render(self.request, ‘test.html’) in b/views.py it is loading the template from a because alphabetically that comes first.


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        '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',
            ],
        },
    },
]

Take a look into this post, it solves the same question.

1 Like

Ohk but the way it uses doesnt make sense to me. Why do we need to create another folder for namespacing. Shouldnt render function deduce the caller app name? Idk there should be some way to get the app name in tempelate loader

I would prefer to use global templates directory

This is by design and on purpose. It’s much better this way.

The benefit to doing it this way is precisely to allow you to override templates defined by other apps.

1 Like