Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Template inheritance: how many levels and what page to render

I would like to have some advice in constructing django template levels.

Reading the docs, I do not understand how to create a template hierarchy structure with more than 2 levels.

For example, I would like to create 3 levels of inheritance:

base.html
    └── base_level2.html
            ├── level2_level3_1.html
            └── level2_level3_2.html

This is my code:

  • base.html

    First level
    {% block level2 %}{% endblock %}
    
  • base_level2.html

    {% extends "base.html" %}
    {% block level2 %}
        Second level
        {% block level3_1 %}{% endblock %}
        {% block level3_2 %}{% endblock %}
    {% endblock %}
    
  • level2_level3_1.html

    {% extends "base_level2.html" %}
    {% block level3_1 %}
        Third level, part 1
    {% endblock %}
    
  • level2_level3_2.html

    {% extends "base_level2.html" %}
    {% block level3_2 %}
        Third level, part 2
    {% endblock %}
    
  • views.py:

    def myView(request):
        return render_to_response('level2_level3_1.html', {}, context_instance=RequestContext(request))
    

In this way I can see the following on my browser:

First level
Second level
Third level, part 1

And this is logical to me because I call render_to response only on level2_level3_1.html.

Of course, if call level2_level3_2.html, I get the message Third level, part 2 but not the Third level, part1.

How to solve this? Is that a good approach? I've structured stuff in this way because my real templates are very big, a lot of lines of code, so I would like to keep some order. Any advice will be appreciated.

like image 629
caneta Avatar asked Jun 03 '13 15:06

caneta


People also ask

How do I use an inheritance template in Django?

The {% extends “base. html” %} inherits everything from the base template. Code between the {% block %} tags above is placed inside {% block %} tag in the base html, so when Django processes the template, this is how the home page is rendered: This process can be repeated for as many pages as we want.

When extends is used for inheriting a template?

extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.

What does {% mean in Django?

{% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What does {{ name }} this mean in Django templates?

8. What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.


2 Answers

Probably not the best way of doing it but you might user include https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include

something like this for base_level2.html

{% extends "base.html" %}
{% block level2 %}
Second level
{% include "level2_level3_1.html" %}   
{% include "level2_level3_2.html" %} 
{% endblock %}

i've not tested this, so not sure it works.

and btw:

The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.

like image 34
EsseTi Avatar answered Oct 29 '22 10:10

EsseTi


It's hard to say if it's a good or bad idea or not without knowing the specific functionality of your templates, but my immediate reaction is that you're trying to over organize your templates. I think most people would urge you away from more than a 3-tier system because it makes it more difficult to make small changes in the website and more difficult to keep track of where things are. from the Zen of Python:

Flat is Better than Nested

The recommendation for a 3-tier system inTwo Scoops of Django goes like this:

  1. Each app has a base_<app_name>.html template. App-level base templates share a common parent, base.html.
  2. Templates within apps share a common parent base_<app_name>.html template.
  3. Any template at the same level as base.html inherits base.html

and for your naming schema, it might look like this:

  | Templates/
  |--base.html
  |--someothertemplate.html # extends base.html
  |--level2/
  |----base_level2.html     # extends base.html
  |----level2_1.html        # extends base_level2.html
  |----level2_2.html        # extends base_level3.html

EDIT: and there's no real reason for this:

    Second level
   {% block level3_1 %}{% endblock %}
   {% block level3_2 %}{% endblock %}

where each block refers to the content of one template. you can simplify that to one block like

{% block level3 %}{% endblock level3%}

and then in each of the level3 templates, rename the blocks accordingly

like image 127
skzryzg Avatar answered Oct 29 '22 10:10

skzryzg