Django: TemplateDoesNotExist error for base template

7.2k views Asked by At

I learnt about Django template inheritance and was working on it.

I made a base_post_login.html template in the same directory as other templates.

and type {% extends "base_post_login.html" %} as the first line in in a child template.

But when the child template is rendered through back-end the TemplateDoesNotExist error is raised.

this is settings.py(relevant part):

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

all templates are rendered properly if it does not extend and parent template.

What should I do?

2

There are 2 answers

4
Seif On BEST ANSWER

You are using Django extends the wrong way, it should take the parent template name so do this:

{% extends "base.html" %}

EDIT

OK I see, you should use the template path like you do when you render your other templates:

Let's say you render like this "templates/child_page.html" then you should extend the same way {% extends "templates/base.html" %}

0
pujan rai On

go to settings.py

your templates settings should look like this:

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',
            ],
        },
}, ]

make sure BASE_DIR is also defined