Search code examples
djangodjango-templatesdjango-sekizai

django sekizai {% addtoblock %} tag is not working properly


I'm trying to implement django sekizai app. It is duplicating the js files that i'm adding.

base template:

{% load sekizai_tags %}
...
{% render_block "my_js" %}

template that is using this base:

{% load sekizai_tags %}
<div id="a1" >
    {% addtoblock "my_js" %}
        <script type="text/javascript" src="{{ MEDIA_URL }}js/my_js.js"></script>
    {% endaddtoblock %}
</div>
{% addtoblock "my_js" %}
    <script type="text/javascript" src="{{ MEDIA_URL }}js/my_js.js"></script>
{% endaddtoblock %}

Now here the rendered template has rendered twice.But when I tried adding the same script within the div it wasn't duplicated. Would appreciate if someone can shed some light on this!

Also when i try to use {% addtoblock %} in a template rendered by a template tag the script goes missing (It is neither included nor it stays in that template).

Note: The template tags, render_block and addtoblock, are from the django-sekizai package.


Solution

  • {% addtoblock %} and {% endaddtoblock %} have to be inside of a block in templates that inherit another template.

    # base.html
    <html>
        ...
        {% render_block 'js' %}
        {% block js %}{% endblock %}
    </html>
    
    
    # some-page.html
    {% inherits 'base.html' %}
    
    {% block js %}
        {% addtoblock 'js' %}
            <script type="text/javascript" ... />
        {% endaddtoblock %}
    {% endblock %}
    

    Hope that helps you out.