开发者

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

I have 3 Django templates:

开发者_JS百科

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?


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.


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...


Django 1.6.6

{{ block.super.super }} - possible

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜