Skip to main content

Wagtail Block Components

Use Wagtail blocks as reusable components in Django templates.

Installation

pip install wagtail-block-components

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'wagtail_block_components',
    # ...
]

Quick Start

1. Define Your Blocks

# myapp/blocks.py
import uuid
from wagtail import blocks
from wagtail.images.blocks import ImageChooserBlock
from wagtail.documents.blocks import DocumentChooserBlock

class AccordionItemBlock(blocks.StructBlock):
    title = blocks.CharBlock(required=True)
    content = blocks.StreamBlock([
        ('paragraph', blocks.RichTextBlock()),
        ('page', blocks.PageChooserBlock()),
        ('image', ImageChooserBlock()),
        ('document', DocumentChooserBlock()),
        ('inline_structblock', blocks.StructBlock([
            ('heading', blocks.CharBlock()),
            ('content', blocks.RichTextBlock()),
        ])),
    ], required=False)

    class Meta:
        template = 'components/accordion_item.html'

class AccordionBlock(blocks.StructBlock):
    items = blocks.ListBlock(AccordionItemBlock())

    def get_context(self, value, parent_context=None):
        context = super().get_context(value, parent_context)
        # Generate unique accordion ID for children to use
        context['accordion_id'] = f"accordion-{uuid.uuid4().hex[:8]}"
        return context

    class Meta:
        template = 'components/accordion.html'

2. Register Components

# myapp/wagtail_hooks.py
from wagtail import hooks
from . import blocks

@hooks.register('register_components')
def register_components():
    return [
        blocks.AccordionBlock,
        blocks.AccordionItemBlock,
    ]

3. Use in Templates

{% load wagtail_block_components %}

{# Self-closing block - empty accordion #}
{% wagtail_block "AccordionBlock" / %}


{# Block with kwargs passed directly #}
{% wagtail_block "AccordionItemBlock" title="Empty Item" / %}


{# Full accordion with all syntax variations #}
{% wagtail_block "AccordionBlock" %}

    {# First item: using block_field for title (template content) #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" %}
            {% block_field "title" %}Question 1{% endblock_field %}
            {% block_field "content" %}
                {% block_field "paragraph" %}<p>Answer 1 part 1</p>{% endblock_field %}
                {% block_field "paragraph" %}<p>Answer 1 part 2</p>{% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

    {# Second item: using kwargs for title #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" title="Question 2" %}
            {% block_field "content" %}
                {% block_field "paragraph" %}<p>Answer 2</p>{% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

    {# Third item: using shorthand block_field with variable #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" %}
            {% block_field "title" title_var / %}
            {% block_field "content" %}
                {% block_field "inline_structblock" %}
                    {% block_field "heading" %}Inline Struct Heading{% endblock_field %}
                    {% block_field "content" %}<p>Inline Struct Content</p>{% endblock_field %}
                {% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

    {# Fourth item: shorthand with string literal + ChooserBlocks #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" %}
            {% block_field "title" "Question 4" / %}
            {% block_field "content" %}
                {% block_field "image" test_image / %}
                {% block_field "document" test_document / %}
                {% block_field "page" test_page / %}
                {% block_field "paragraph" %}<p>ChooserBlocks work!</p>{% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

{% endwagtail_block %}

How It Works

The tag processes your template syntax into the raw value format Wagtail blocks expect, then calls block.to_python() and block.render().

Syntax Options

Passing Field Values

As kwargs when the value is simple:

{% wagtail_block "AccordionItemBlock" title="Question 1" %}
    {# ... #}
{% endwagtail_block %}

As block_field with content when you need template rendering:

{% wagtail_block "AccordionItemBlock" %}
    {% block_field "title" %}Question {{ question_number }}{% endblock_field %}
    {# ... #}
{% endwagtail_block %}

As block_field shorthand when passing simple values or variables:

{% wagtail_block "AccordionItemBlock" %}
    {% block_field "title" "My Title" / %}
    {# Or from context: #}
    {% block_field "title" my_title_var / %}
{% endwagtail_block %}

Block Types

StructBlock - Use {% block_field %} for each field:

{% wagtail_block "AccordionItemBlock" title="Question" %}
    {% block_field "content" %}
        {% block_field "paragraph" %}<p>Answer</p>{% endblock_field %}
    {% endblock_field %}
{% endwagtail_block %}

ListBlock - Repeat {% block_field %} with the same name:

{% wagtail_block "AccordionBlock" %}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" title="Q1" %}...{% endwagtail_block %}
    {% endblock_field %}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" title="Q2" %}...{% endwagtail_block %}
    {% endblock_field %}
{% endwagtail_block %}

StreamBlock - Use nested {% block_field %} for typed content:

{% block_field "content" %}
    {% block_field "paragraph" %}<p>First paragraph</p>{% endblock_field %}
    {% block_field "paragraph" %}<p>Second paragraph</p>{% endblock_field %}
    {% block_field "image" my_image / %}
    {% block_field "page" my_page / %}
    {% block_field "document" my_document / %}
    {% block_field "inline_structblock" %}
        {% block_field "heading" "Nested Heading" / %}
        {% block_field "content" %}<p>Nested content</p>{% endblock_field %}
    {% endblock_field %}
{% endblock_field %}

ChooserBlock fields (PageChooserBlock, ImageChooserBlock, DocumentChooserBlock) - Pass pk or model instance:

{# Using pk #}
{% block_field "page" 123 / %}

{# Using model instance (pk extracted automatically) #}
{% block_field "page" my_page / %}

Use in Both Places

The same blocks work in Wagtail StreamFields:

from wagtail.models import Page
from wagtail.fields import StreamField
from myapp.blocks import AccordionBlock

class HomePage(Page):
    body = StreamField([
        ('accordion', AccordionBlock()),
    ])

And in regular Django views:

{% load wagtail_block_components %}

{% wagtail_block "AccordionBlock" %}
    {# ... #}
{% endwagtail_block %}

Metadata

Release files for wagtail-block-components 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wagtail-block-components 0.1.0
File Size Uploaded Provenance Actions
wagtail_block_components-0.1.0.tar.gz 10.9 kB With provenance Details for wagtail_block_components-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wagtail-block-components 0.1.0
File Interpreter ABI Platform Actions
wagtail_block_components-0.1.0-py3-none-any.whl Python 3 none any Details for wagtail_block_components-0.1.0-py3-none-any.whl

Total release size: 19.4 kB

Release files / wagtail_block_components-0.1.0.tar.gz

Download URL wagtail_block_components-0.1.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4baf971d6659eab1bf6ec4887122cfa7bdeb837b251a908f06ac88ac0045b4c1
BLAKE2b-256 checksum
How to use checksums
fdc7ee79ceaf05a83d5e910407e06b88e904ddc4e624efec455653c2cb739dc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2025.

Transparency log

Release files / wagtail_block_components-0.1.0-py3-none-any.whl

Download URL wagtail_block_components-0.1.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04a61e555bbda1e3afbdcf548da17eded598bce514bd059a4e9556dc1d248e26
BLAKE2b-256 checksum
How to use checksums
402c7227794ac471a088644f581e2e4ba30a384d9b4ef44c14bca96d4d74a9b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2025.

Transparency log

Security for wagtail-block-components 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for wagtail_block_components-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page