(SOLVED) Issue with variable scope using template inheritance

Hello all,

I’m struggling with the scope of context variables in the Django template inheritance setup.
I use a template in three different places and these templates are all the same except they use a different permission check.

My templates look like this (simplified):

_item_base.html

{% load permission_templatetag_1 %}

{% block load_our_tag %}
{% endblock load_our_tag %}

{% block load_our_title %}
{% endblock load_our_title %}

<table>
    <tr>
        <th>Something 1</th>
        <th>Something 2</th>
    </tr>
    <tr>
        <td>
            {% block load_our_secret %}
                What?
            {% endblock load_our_secret %}
        </td>
        <td> Very basic
        </td>
    </tr>
</table>

item_page_1_of_3.html

{% extends "_item_base.html" %}
{% load permission_templatetag_1 %}

{% block load_our_tag %}
    <!-- this_tag_checks_permission returns context["yes_you_have_permission"] = True if user passes a test -->
    {% this_tag_checks_permission what='1' user=user %}
    <!-- I have checked: yes_you_have_permission is True here -->
{% endblock load_our_tag %}

{% block load_our_title %}
    <!-- however in this block yes_you_have_permission is not True (False/None/'') -->
    {% if yes_you_have_permission %}
        <h1>I'll show you a secret</h1>
    {% else %}
        <h1>Nothing to see here.</h1>
    {% endif %}
{% endblock load_our_title %}

{% block load_our_secret %}
    <!-- and in this block yes_you_have_permission is also not True (False/None/'') -->
    {% if yes_you_have_permission %}
        <p>42</p>
    {% else %}
        <p1>sun</p>
    {% endif %}
{% endblock load_our_secret %}
  • If I don’t use the template inheritance option it all works fine.
  • If I put {% this_tag_checks_permission what='1' user=user %} in each block it also works fine but then I get a DB hit on every block.

What am I doing wrong? What have I missed or don’t understand?

Thanks.

Django 6.0.3
Python 3.14.5

Quoting from the docs at How to create custom template tags and filters | Django documentation | Django

Variable scope in context
Any variable set in the context will only be available in the same block of the template in which it was assigned. This behavior is intentional; it provides a scope for variables so that they don’t conflict with context in other blocks.

In general, you’re really better off setting this in the view and not the template.

Right! That is what I missed.

I’ll take your advice and put it in the view.

Thanks.