Can we append to a {% block %} rather than overwrite?

DjangoDjango Templates

Django Problem Overview


In my core.html I have a block labeled javascript. It would be great if I can append more lines to this block without overwriting everything in it.

Django Solutions


Solution 1 - Django

{% block javascript %}
    {{ block.super }}
    ... more content ...
{% endblock %}

See: Django documentation - Template inheritance

Solution 2 - Django

Using block.super works fine when extending a template but not as well when including one, ie:

{% extends "base.html" %} vs. {% include "partial.html" %}

Say you want to include a template in the middle of your page and you'd also like it to add some javascript in a block at the end of the page: calling block.super in the included template will crash.

Cf. Django issues #7324, #12008, #13399 and the related update to the documentation. Cf. include tag note:

> 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. > > Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.

In that case I'd recommend using django-sekizai, wich allow you to do things like:

{% load sekizai_tags %}

                                ⎧  <p>Some content</p>
<p>Some content</p>             | {% addtoblock "js" %}
                                |   <script type="text/javascript">
{% include "partial.html" %} -> ⎨     alert("Hello django-sekizai");
                                |   </script>
<p>Some more content</p>        ⎩ {% endaddtoblock %}


{% render_block "js" %}

From django-sekizai README:

> The main reason I started this project was the lack of a good media (css/js) framework in django and the django-cms. Yes there is the Media class used in forms in django, but really that doesn't work that well. Usually the frontend guys want to decide on css and javascript files to be included and they don't want to have to edit Python files to change that neither did I want them to change my Python files. Therefor there was a need to allow you to edit contents of templates which are before or after the point where you are now. Also I wanted duplicates to be removed. As a result I wrote django-sekizai, which does exactly that. It's similar to blocks, just instead of inheriting them, you extend them.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPKKidView Question on Stackoverflow
Solution 1 - DjangoSteve LoshView Answer on Stackoverflow
Solution 2 - DjangoMaxime R.View Answer on Stackoverflow