How to access template block from plugin within Django CMS Placeholder

Question

I have the following code:

base.html

<html>
...
{% block test_block %}
    {# Some stuff to render #}
{% endblock %}
...
</html>

main_template.html

{% extends "base.html" %}    
...
{% block content %}
    {% placeholder "content" %}
{% endblock %}
...

plugin.html

...

If I add my plugin to the page it renders in the placeholder block, as expected.

If I amend plugin.html to add the following:

plugin.html

...
{% block test_block %}
    {{ block.super }}
    {# Some more stuff to render #}
{% endblock %}
...

Then I receive an error:
'BlockNode' object has no attribute 'context'. Did you use {{ block.super }} in a base template?

If I try and extend either main_template.html or CMS_TEMPLATE (they seem to be the same?) from within plugin.html I get the following error:
maximum recursion depth exceeded while calling a Python object

How can I access and append to test_block from within my plugin?


Show source
| django   | django-cms   2017-01-04 14:01 1 Answers

Answers ( 1 )

  1. 2017-01-05 11:01

    In order to use a block you must, I believe, do so in a template which extends from a template where that block is defined.

    However, to do what you want, CMS makes use of django-sekizai.

    With this you can have this in base.html;

    {% load sekizai_tags %}
    <html>
        ...
        {% render_block "test_block" %}
        ...
    </html>
    

    And then in plugin.html you can do;

    {% load sekizai_tags %}
    {% addtoblock "test_block" %}
        Add this to my block
    {% endaddtoblock %}
    

    This is often used for CSS & JS blocks, but works perfectly well for what you want to do.

◀ Go back