Question

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?

Was it helpful?

Solution

I don't think this is possible. Just for your specific problem I guess it can be solves if you would place the site name before the block, and use the block just to append something to the site name.

Otherwise you could define SITE_NAME in your settings.py and have a context processor like

from django.conf import settings
def site_name(request):
    return {'SITE_NAME': settings.SITE_NAME}

so that you can use {{ SITE_NAME }} in your templates - this could make sense because the site name could be usefule at other places too...

OTHER TIPS

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.

Django 1.6.6

{{ block.super.super }} - possible

I try now - worked :) But not officially ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top