Mikael Svensson
Mikael Svensson

Reputation: 720

Wagtail blocks: accessing context and request in overridden get_context

I trying to get the page and the request from context to be able to use pagination inside the block. The only context i get is

context {'self': None, 'value': None}

Is it even possible to have pagination inside a streamfield block?

class CustomStaticBlock(blocks.StaticBlock):


    def get_context(self, value):
        context = super(CustomStaticBlock, self).get_context(value)

Rendering with

{% include_block block%}

Upvotes: 3

Views: 3626

Answers (2)

François Constant
François Constant

Reputation: 5496

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 }}

Upvotes: 6

gasman
gasman

Reputation: 25227

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).

Upvotes: 4

Related Questions