[Django]-Wagtail blocks: accessing context and request in overridden get_context

6👍

It’s now working (within get_context). If someone has the same issue with a StreamField, make sure to render it as:

{% for block in page.body %}
{% include_block block %}
{% endfor %}

The following won’t work (empty parent_context):

{% include_block page.body %}

{{ page.body|safe }}

4👍

The context from the outer page is available within the block template, but unfortunately not within the get_context method. (This is due to the way the template context is built up – the result of get_context is merged into the parent context.) This is a known limitation:

https://github.com/wagtail/wagtail/pull/2786#issuecomment-230416360
https://github.com/wagtail/wagtail/issues/2824

A possible workaround would be to override the render method (which is admittedly not ideal, because you’d have to repeat some or all of the existing render logic there).

👤gasman

Leave a comment