Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Django, Problem with {{ block.super }}, How do I avoid duplicating a `block` in multiple template files?
    primarykey
    data
    text
    <p>For 2 child template files inheriting a block, the <code>{{ block.super }}</code> does not resolve</p> <p>Python 2.5.2, Django 1.0, Windows XP SP3</p> <p>Sample skeleton code for files involved:</p> <ol> <li><code>base.html</code></li> <li><code>item_base.html</code></li> <li><code>show_info_for_all_items.html</code></li> <li><code>show_info_for_single_item.html</code></li> </ol> <p>FILE : <code>base.html</code></p> <pre><code>{% block content %} {% endblock %} </code></pre> <p>FILE : <code>item_base.html</code></p> <pre><code>{% extends "base.html" %} {% block item_info %} Item : {{ item.name }}&lt;br/&gt; Price : {{ item.price }}&lt;br/&gt; {% endblock %} </code></pre> <p>FILE : <code>show_info_for_all_items.html</code></p> <pre><code>{% extends "item_base.html" %} {% block content %} &lt;h1&gt;info on all items&lt;/h1&gt; &lt;hr/&gt; {% for item in items %} {% block item_info %} {{ block.super }} {% endblock %} &lt;hr/&gt; {% endfor %} {% endblock %} </code></pre> <p>FILE : <code>show_info_for_single_item.html</code></p> <pre><code>{% extends "item_base.html" %} {% block content %} &lt;h1&gt;info on single item&lt;/h1&gt; {% block item_info %} {{ block.super }} {% endblock %} {% endblock %} </code></pre> <p><code>show_info_for_all_items.html</code> shows a list of items along with each item's info.</p> <p><code>show_info_for_single_item.html</code> shows a single item with the item's info.</p> <p><code>show_info_for_all_items.html</code> and <code>show_info_for_single_item.html</code> share same code for showing item info, so I moved it to <code>item_base.html</code> into <code>block item_info</code></p> <p>but the <code>{{ block.super }}</code> in <code>show_info_for_all_items.html</code> and <code>show_info_for_single_item.html</code> does not work. <code>{{ block.super }}</code> resolves as blank.</p> <p>If I move the code back from <code>block item_info</code> in <code>item_base.html</code> into <code>show_info_for_all_items.html</code> and <code>show_info_for_single_item.html</code> it works but then I have to duplicate same <code>block item_info</code> code in 2 files.</p> <p>If the block.super issue can not be solved, does Django offer something like INCLUDE => <code>{% INCLUDE "item_base.html" %}</code> so blocks from a template file can be included ( instead of <code>extends</code> )</p> <p>How do I avoid duplicating <code>block item_info</code> in both html files?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload