Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Extends or Include?

My friend and I are having a small argument. In my current Django Project, I have created a file called menu.html which will contain a bunch of links configured and formatted into a list. Instead of manually hard-coding the menu into each page, I am currently including the menu using the following Django/Python code:

{% include 'menu.html' %}

However, my friend is suggesting that this is the incorrect way to do it. He said I need to use extends instead of include and then define content, something like this:

{% extend 'menu.html' %}
{% block content %}
The rest of my content here.
{% endblock %}

That's a bit of extra code. Does it really matter which I use? I would prefer to use the former.

like image 794
Reznor Avatar asked Feb 24 '10 01:02

Reznor


People also ask

What does %% include?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

What does {% include %} do in Django?

The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.

Why we use extends in Django?

The extends tag allows you to add a parent template for the current template.

What does include does in Django?

include tag loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.


1 Answers

Yes, it matters. First of all extends can only occur as the very first line of the file. Secondly, include pushes and pops a context object on the resolve stack, which means that value created in the context while in the include will go out of scope when it returns.

My rule is: create base.html template files that define the overall structure of your site and use liberal amounts of {% block foo %} around critical areas. Then all of your other templates extends the base (or something that itself extends the base) and you replace those blocks as needed.

include, on the other hand, is good for encapsulating things you may need to use in more than one place, maybe even on the same page.

Update:

I have been using my own library of template_tags for so long that I forget that Django's template language still has major gaps in functionality. The tag in question here is from an early django snippet called expr which I have heavily edited and extended. You can say, for example, {% expr 'Fred' as name %} (or any valid Python expression), and it will store the result in the 'name' slot in the current Context. If this occurs in an included template, name's value will be popped on exit from the template file.

You can sort of achieve this with the {% with %} tag, but expr gives me much greater flexibility, including doing arbitrarily complex calls. This originally came up when having to create complex cached objects that required expensive DBMS interactions that couldn't be done up in the view, they had to be invoked in the template itself.

Email me (in my profile) if you need to get deeper into this.

like image 142
Peter Rowell Avatar answered Sep 20 '22 09:09

Peter Rowell