day
day

Reputation: 1157

Filepath for Django Extending Template

Here is my template path

webapp/
  |__templates/
        |__frontpage/
        |     |__home.html
        |__home_base.html 

In home.html, it has:

{% extends "home_base.html" %}

This file structure will work. However, I want to put home_base.html inside frontpage/, as this makes more sense. However, Django will report home_base.html TEMPLATE DOES NOT EXIST, if home_base.html is moved to frontpage/.

The error says it cannot find the home_base.html file under templates/ folder. Since the home_base.html is moved to frontpage/, why doesn't it search for home_base.html inside frontpage/ first? Any configurations I am missing?

Upvotes: 1

Views: 206

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47846

You need to do the following for template to be extended.

{% extends "frontpage/home_base.html" %}

Django does not have an idea where you have have moved your template. It will look for the template according to the templates path you have defined in your settings.

The template loader will look for template in the directories defined in the DIRS setting in the TEMPLATES settings.

From the DIRS setting documentation:

Directories where the engine should look for template source files, in search order.

Also, if frontpage was an app and you had placed your template in the frontpage app instead of the templates folder, then you can set the APP_DIRS settings to be True. This will tell Django to find the templates in the individual apps also.

From the APP_DIRS setting documentation:

Whether the engine should look for template source files inside installed applications.

Upvotes: 1

Related Questions