1

I have my index.html file in /templates directory and I have another index.html located in /templates/hello.

I've created a file named templates.html in /templates/hello and it should extend index.html.

Can I make template.html extends both index.html files (from both directories) using {% extends index.html %} tag in it?

Thanks.

M.javid
  • 6,387
  • 3
  • 41
  • 56

1 Answers1

3

You cannot extend from multiple django templates.. it's a single line inheritance.

If you want /templates/index.html to be your base index template, and /templates/hello/index.html to be your index template for the hello part of your application.. then you should have /templates/hello/index.html start with {% extends 'index.html' %}.

The thing to understand with Django templates is that the base template.. the one that is 'extended', is THE template.. and everything in that template will be displayed whether it is within a block tag or outside one.

When you 'extend' a template, any blocks declared which match blocks in the template that was extended, will override the content of those blocks.

Most web sites/applications have a more or less consistent layout from page to page. So, a typical template setup would be to have a master template that contains blocks for all the various parts of the page, with divs and css to arrange the layout the way you want. Put as much as the common html.. the stuff that does not change often from one page to the next, in that base template, and make sure the base template contains blocks for anything you need to fill in when you extend the template. These blocks can contain default html which will be shown if the extending template does not override that block. Or they can be empty.

Then, for each new template variation that you need, extend the master template and override only those blocks that need to be filled in or overrriden.

Don't think of the extend as bringing the code of your base template into the template that is extending it.. Django templates do not work like that. Think of the base template as THE template which has all the basic building blocks of your page, and then the extension MODIFIES the blocks of the template that it extends.

If you have a different situation where the pieces of your page need to be defined in different templates and you wish to piece them together, then what you are looking for is the {% include 'templatename' %} tag.

little_birdie
  • 5,600
  • 3
  • 23
  • 28
  • Thanks for the reply. So, I have tried to extends only from the index.html from /templates/hello, but whenever I put {% block blash %}{% endblock%} in index.html, it just shows a blank page. Any idea what I'm doing wrong? NOTE: my template.html exists in same directory. – Toshihiro M Sakurai Sep 11 '15 at 01:35
  • It seems that your template extends index.html which is in `/templates/` directory, not in `/templates/hello` as Django finds it first. You should use namespacing in extends tag: {% extends 'hello/index.html' %} – chem1st Sep 11 '15 at 02:00
  • I have tried it, but did not work. It's just displaying blank page... which seems odd. I was expecting some errors to show up. – Toshihiro M Sakurai Sep 11 '15 at 02:42
  • To say smth more specific, show your `templates/hello/index.html`, `templates/hello/template.html` and the list of template dirs your django knows - to get it in a traceback, just set some not existing template in your view with debug on. – chem1st Sep 11 '15 at 07:13
  • Please tell us what you're trying to accomplish exactly that isn't working out. It'll be a lot easier to help. I've updated my answer to make the whole inheritance thing more clear. Basically, anything that is in a block in your base template will be overridden by a block of the same name in any template that extends it. – little_birdie Sep 11 '15 at 20:58