Saurabh Dwivedi
Saurabh Dwivedi

Reputation: 39

Django include tag

We are using django with python. I am facing a problem with include tag. I want to include a header in all modules of application. In application templates directory contains all the html files with subfolders of modules. In any sub-module if I am creating header html and including this tag in base.html then it is working. But if I place the header html in parent directory, its not working for any sub-modules. I even tried {% include "../header.html" %} in html with django template, but no luck.

The project structure fo my application, in which root directory I have templates, static, handlers folder. Inside templates I have sub1 and sub2 folders. In sub1 I have base.html and in templates parent directory base.html, header.html, index.html. See below:

Root
----templates
----------Sub1
-----------------base.html
----------Sub2
----------base.html
----------header.html
----------index.html
----static
----handlers

Upvotes: 1

Views: 4444

Answers (2)

Amyth
Amyth

Reputation: 32949

Django templates include tag does not recognize relative paths. You need to give it the path under your templates directory, so try using the following instead:

{% include "Sub2/header.html" %}

Update:

Seems like your "header.html" are on the "Sub2" level and not inside it i.e. it's directly under templates directory So you should try:

{% include "header.html" %}

Upvotes: 3

Leon
Leon

Reputation: 1995

I am not sure if I have understood you. Just try {% include "header.html" %}, the search of django template should start from template folder.

The template folder is configured in Django config file "setting.py".

TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

In addition, I have a project which can seprate Django template develop from the back-end using webpack and support jade, es6 and scss. The readme is in chinese :(, but you can run the project and check the source code.

https://github.com/njleonzhang/webpack-django-starter

Upvotes: 0

Related Questions