开发者

Django: How to include a block site wide?

Apologies, this is pretty basic. I have abstracted my static html naviga开发者_C百科tion bar to a block so that it can be dynamically rendered from a model. I have created a new block marker in base.html with the following syntax

{% block navigation %}{% endblock %}

How can I ensure this is rendered on every page? Do I need to create some sort of middle layer for this? Everything I have done so far has simply used the primary block.

EDIT Sunday, 14 August 2011 11:25 AM

I didn't explain this very well. The content of navigation block is

{% extends 'base.html' %}

{% block navigation %}

<nav>
    <ul>
    {% for item in items %}
        <li><a href="{{ item.url }}">{{ item.name }}</a></li>
    {% endfor %}
    </ul>
</nav>

{% endblock %}

I want to render this on every page without having to go through child templates of base.html individually and add it to them, if that is possible.


You have got static navigation bar, so you can just write code in your base.html page and then use it with {% extends 'base.html' %} tag on every new page. Your markup will be in every page. Also, if you use only extends tag in your child page and then render it - you will see base.html without any edition.

If you have got code, which need to appear on few pages, but not on everyone - create 'includes' directory, save code there and extend your base template with {% include %} tag. It won't avoid repating, but make your code shorter.

And last thing you'll need in future, maybe with dynamic code - caching. With {% cache %} tag you can cache block for some time.


Your block is not doing anything here, but if you want something to appear in all the templates, you have to define it in a base template (base.html) and make the others extend from it:

base.html

Yor menu and the stuff you want to appear everywhere
{% block content %}{% endblock %}

Another template

{% extends "base.html" %}
{% block content %}
The actual content of the page
{% endblock %}


{% block FOO %}{% endblock %} reserves a space to be overwritten in sub templates.

{% include "foo.html" %} pulls content from another file into the current file.

To get {% block navigation %}{% endblock %} to display on every page, you need to add content

{% block navigation %}SHOW ME{% endblock %}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜