Append to site <title> in Django template using block.super

San4ez picture San4ez · Dec 1, 2010 · Viewed 9.3k times · Source

I have 3 Django templates:

base.html

<title>{% block title %} SITE NAME {% endblock %}</title>

default.html

{% extends "base.html" %}
{% block title %} {{ block.super }} - SECTION NAME {% endblock %}

main.html

{% extends "default.html" %}
{% block title %} {{ block.super }} {% endblock %}

I'd like to get SITE NAME in template main.html i.e. the content of the parent of the parent block. Something like

{{ block.super.super }}

Is this possible?

Answer

Cerin picture Cerin · Dec 20, 2010

Note, Django 1.2.3 seems to already do what you want. Assuming SITE_NAME is exposed via a context_preprocessor like lzerscience illustrates, block.super should expose it through all the layers of inheritance.

main.html

{% extends "default.html" %}
{% block title %} {{ block.super }} - MAIN{% endblock %}

That displays the title "SITE NAME - SECTION NAME - MAIN" for me.