[Django]-Django multi-level template extends and nested blocks

55👍

No, but you can use {{ block.super }}:

{% block extra_head_content  %}
    {{ block.super }}
    <link rel="stylesheet" type="text/css" href="/static/css/account.css" />
{% endblock %}

3👍

The django-sekizai module takes care of addition to css and javascript with ease:

#base.html
{% load sekizai_tags %}
# define your template, declaring blocks for inheriting templates:
{% block content %}
{% endblock content %}
# at the bottom of the body:
{% render_block "js" %}
</body>
</html>

#my_template.html
{% extends "base.html" %}
{% load sekizai_tags %}
{% block content %}
# content goes here...
# so does the addtoblock tag
    {% addtoblock "js" %}
        <script src="my/awesome/script.js"></script>
    {% endaddtoblock %}
{% endblock content %}
# Note no addtoblock tags outside the block-endblock tags

The sekazai docs make clear the caveats for using this system, namely that:

  1. render_block should only be used outside block tags
  2. render_block cannot be used in included templates
  3. addtoblock should be used inside block tags when used in an included template

Leave a comment