[Fixed]-Django template structure issues

1👍

You can just use {% extends 'base.html' %}. All template directories contribute to the same namespace. It doesn’t matter in which template directory the base template is, the only thing that matters is its relative path from the template directory.

This is also why you should use a prefix for app-specific templates. If you didn’t, and had both project/templates/base.html and app1/templates/base.html, Django would see them as the same file, with one overriding the other.

The TEMPLATE_DIRS setting is deprecated in favour of TEMPLATES, and if the latter is set, TEMPLATE_DIRS is ignored. You should add the directory to 'DIRS' instead:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(PROJECT_ROOT, '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',
            ],
        },
    },
]
👤knbk

Leave a comment