Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{% include %} vs {% extends %} in django templates

When particularly extend template and when to use include ? Is include of any use with content like user profile section (like about me in the corner of our site) ?

like image 221
sasquatch Avatar asked May 19 '10 07:05

sasquatch


People also ask

What is the difference between extends and include in Django?

Extends sort of 'includes' the parent template and then can overwrite parts of it for different functionality. Include does a simple include rendering a template in a current context.

What does {% include %} in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What does {% include %} do?

Q13:-What does {% include %} does? It will include another template. It will include content from another template having the same templates defined.

Why is {% extends %} tag used?

The extends tag is used to declare a parent template. It should be the very first tag used in a child template and a child template can only extend up to one parent template. To summarize, parent templates define blocks and child templates will override the contents of those blocks.


3 Answers

Extending allows you to replace blocks (e.g. "content") from a parent template instead of including parts to build the page (e.g. "header" and "footer"). This allows you to have a single template containing your complete layout and you only "insert" the content of the other template by replacing a block.

If the user profile is used on all pages, you'd probably want to put it in your base template which is extended by others or include it into the base template. If you wanted the user profile only on very few pages, you could also include it in those templates. If the user profile is the same except on a few pages, put it in your base template inside a block which can then be replaced in those templates which want a different profile.

like image 65
ThiefMaster Avatar answered Sep 23 '22 15:09

ThiefMaster


See about django template inheretance.

Extends sort of 'includes' the parent template and then can overwrite parts of it for different functionality.

Include does a simple include rendering a template in a current context.

like image 35
Unreason Avatar answered Sep 24 '22 15:09

Unreason


extends creates "parent child relation". There is a chance of over-writing of parent functionality in case of extends. While include simply renders the html response.

like image 33
MUHAMMAD AWAIS BIN MAJID Avatar answered Sep 20 '22 15:09

MUHAMMAD AWAIS BIN MAJID