django template inheritance: how to NOT display block from parent?
Working on my first Django project. New to templates and inheritance.
I'm using Bootstrap and want a splashy homepage. So I don't want sidebars, just Jumbotron. But, my index.html inherits from base.html and displays my sidebars which I do want in every other page but my home page. I want everything else, nav, footer, etc. to inherit.
My base.html:
{% block right_panel %}
blah blah blah
{% endblock %}
Is there a way to not inherit this block in my index.html? Or do I make a standalone index.html template with all of the block from base.html minus those I don't want to display?
What's best practice?
EDIT
Here's the offending piece in base.html:
<div class="col-md-3 right">
{% nevercache %}
{% include "includes/user_panel.html" %}
{% endnevercache %}
<div class="panel panel-default">
<div class="panel-body">
{% block right_panel %}
{% ifinstalled mezzanine.twitter %}
{% include "twitter/tweets.html" %}
{% endifinstalled %}
{% endblock %}
</div>
</div>
</div>
The CSS is rendering: <div class="panel-body">
My page.html:
{% extends "base.html" %}
<!-- no right-panel content-->
{% block right_panel %}{% endblock %}
{% load mezzanine_tags keyword_tags %}
{% block meta_title %}{{ page.meta_title }}{% endblock %}
{% block meta_keywords %}{% metablock %}
{% keywords_for page as keywords %}
{% for keyword in keywords %}
{% if not forloop.first %}, {% endif %}
{{ keyword }}
{% endfor %}
{% endmetablock %}{% endblock %}
{% block meta_description %}{% metablock %}
{{ page.description }}
{% endmetablock %}{% endblock %}
{% block title %}
{% editable page.title %}{{ page.title }}{% endeditable %}
{% endblock %}
{% block main %}
{% endblock %}
When I add:
`{% block right_panel %}{% endblock %}
to the top of page.html, the content doesn't render.
What's the best approach? Should I make a new block and wrap it around the that is being styled and then leave it empty in other templates? Or should I move the offending chunk from base.html to another template file and include it on pages where I want it rendered.
Also, another thing. If I remove {% block right_panel %}{% endblock %}
from page.html and put it in gallery.html which inherits from page.html, the content still renders.
Just declare the block as an empty block in your index.html to 'mute' the content from the base.html:
ie, in index.html:
...
{% block right_panel %}{% endblock %}
...
For clarity, if you block some code in base.html
:
{% block right_panel %}
<div class="footerlinks">
<ul>
<li><a href="#">about us</a></li>
<li><a href="#">contact us</a></li>
<li><a href="#">help</a></li>
</ul>
</div>
{% endblock %}
It will show up in all templates that extend base.html
, unless the block is overridden, thereby replacing the block.
Thus,
{% block right_panel %}{% endblock %}
Will effectively mute the section. It's up to you to then build a replacement into your template that will keep everything in one place.
If your efforts are moving things around on your page, it may be wise to only block the visible parts of the section:
<div class="footerlinks">
{% block right_panel %}
<ul>
<li><a href="#">about us</a></li>
<li><a href="#">contact us</a></li>
<li><a href="#">help</a></li>
</ul>
{% endblock %}
</div>
链接地址: http://www.djcxy.com/p/90134.html
上一篇: Django模板的简单继承问题