wagtailwagtail-streamfieldwagtail-streamblock

Wagtail, how to render the blocks in a StreamBlock inside a StreamField


I created a number of custom StructBlocks that are available through a StreamBlock like so:

class ContentStreamBlock(blocks.StreamBlock):
    sub_heading = SubHeadingBlock()
    paragraph = RichTextBlock()
    image = ImageBlock()
    quote = QuoteBlock()

    class Meta:
        template = "blocks/content_stream_block.html"

This is used on the Page model:


class ContentPage(Page):
    body = StreamField([
        ('block', ContentStreamBlock()),
    ], use_json_field=True)
    content_panels = Page.content_panels + [
        FieldPanel('body'),
    ]

However, when rendering the page it shows every element without the values.
The file blocks/content_stream_blocks.html is built like this:

{% load wagtailcore_tags wagtailimages_tags %}

<div class="mt-16 max-w-2xl">
    <h2>new block</h2>
    {% for blk in block.value %}
        {% include_block blk %}
    {% endfor %}
</div>

The custom StructBlocks like ImageBlock etc work fine directly under the StreamField, but break when included in the subclassed StreamBlock.

What am I missing?


Solution

  • Within a block template, the block value is available as the variable value, not block - so in the line {% for blk in block.value %}, block.value is undefined. It should be: {% for blk in value %}

    EDIT:

    Below full example seem to works great.

        {% for block in value %}
            {% include_block block %}
        {% endfor %}