3

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?

sachsure
  • 828
  • 1
  • 17
  • 30
  • Your question is not clear. Can you explain how you structuring your application and mention where your templates folder located... – Raja Simon Dec 21 '16 at 11:16
  • looks like your child template and `base_post_login.html` are not in same directory. And you set `APP_DIRS` to `True`. Do your template files exist under different apps' folders? If so, you may need to define those solders in `DIRS` option. – alioguzhan Dec 21 '16 at 11:19
  • @alix He said > in the same directory as other templates – Seif Dec 21 '16 at 11:20
  • I know that.. But something is not clear and missing in question. The code should work with this structure. @Saksow – alioguzhan Dec 21 '16 at 11:22
  • check my answer update, it may be that – Seif Dec 21 '16 at 11:23

2 Answers2

7

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" %}

Seif
  • 1,058
  • 11
  • 19
  • sorry for that..it was a typo..fixed now..it is base_post_login.html – sachsure Dec 21 '16 at 11:14
  • 1
    oh yeah..this worked ..thanks..i thought that it is not required as both the templates are in the same directory but they are rendered through views. – sachsure Dec 21 '16 at 11:32
  • Actually it doesn't matter where each template is, you have to refer to it using your template path, the same thing you do in views you do it in templates. Cheers! – Seif Dec 21 '16 at 12:05
0

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

pujan rai
  • 9
  • 2