Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested blocks in Django templates

The master template in my Django app looks like this:

{% block parent %}     Some text...     {% block child %}         Default content here...     {% endblock child %}     ...some more text {% endblock parent %} 

Now, this template should be overwritten in a way that the child block is changed:

{% extends "master.html" %}  {% block child %}     New content here... {% endblock child%} 

However, the rendering stays the same (printing "default content here..."). Have I missed something obvious or are nested blocks not possible? (Or, violating the DRY principle, have I to re-define the parent block?)

Edit: I'm working with Django 1.1, if that matters.

like image 611
Boldewyn Avatar asked Dec 14 '09 13:12

Boldewyn


People also ask

What does {% %} mean in Django?

{% 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 is block super in Django template?

From the Django documention: "If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it.

What {% include %} does?

{% 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 are templates in Django?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.


1 Answers

OK, it's a bug in Django's template system. For most other cases, Ben James is right (see his comment to my question above).

In my case, the child block was inside a {% ifnotequal a b %} block, and that breaks the block inheritance. I consider that to be a bug, since there are dozens of natural use cases for such operations.

The corresponding ticket.

like image 78
Boldewyn Avatar answered Oct 02 '22 12:10

Boldewyn