4
{% extends "Flow/base.html" %}

{% if no_tracking %}
    {% block head %}
        {% include "Flow/common/tracking/disabled.html" %}
    {% endblock %}
{% endif %}

The snippet of code aboves overrides the head block in base.html, even though no_tracking is False. How can I make this behavior conditional?

I thought of this:

{% block head %}
    {% if no_tracking %}
        {% include "Flow/common/tracking/disabled.html" %}
    {% else %}
    <!-- How can I get "head" of base.html here? -->
    {% endif %}

{% endblock %}

But this would also override the head of base.html.

head of base.html is not empty, it contains scripts that must be on the page when no_tracking is False.

I could override them in base.html, but base.html has no concept of no_tracking since that is a context variable passed to the view being rendered and not the one it extends.

How can I solve this?

1 Answer 1

6

You can make use of {{ block.super }} [Django-doc] to obtain the content that is rendered by the "parent" block:

If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{ block.super }} will not be automatically escaped (…), since it was already escaped, if necessary, in the parent template.

So here we can use it to add some "extra content" to the block:

{% block head %}
    {% if no_tracking %}
        {% include "Flow/common/tracking/disabled.html" %}
    {% endif %}
    {{ block.super }}
{% endblock %}
Sign up to request clarification or add additional context in comments.

1 Comment

Man, you're like a Django-god... if I google "Django docs" it should say "were you looking for 'Van Onsem' instead?". Thank you! Will accept asap.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.