W3cubDocs

/Wagtail

StreamField block reference

This document details the block types provided by Wagtail for use in StreamField, and how they can be combined into new block types.

Changed in version 3.0: The required use_json_field argument is added.

body = StreamField([
    ('heading', blocks.CharBlock(form_classname="title")),
    ('paragraph', blocks.RichTextBlock()),
    ('image', ImageChooserBlock()),
], block_counts={
    'heading': {'min_num': 1},
    'image': {'max_num': 5},
}, use_json_field=True)

Block options

All block definitions accept the following optional keyword arguments:

  • default

    • The default value that a new ‘empty’ block should receive.
  • label

    • The label to display in the editor interface when referring to this block - defaults to a prettified version of the block name (or, in a context where no name is assigned - such as within a ListBlock - the empty string).
  • icon

    • The name of the icon to display for this block type in the menu of available block types. For a list of icon names, see the Wagtail style guide, which can be enabled by adding wagtail.contrib.styleguide to your project’s INSTALLED_APPS.
  • template

    • The path to a Django template that will be used to render this block on the front end. See Template rendering
  • group

    • The group used to categorize this block. Any blocks with the same group name will be shown together in the editor interface with the group name as a heading.

Field block types

Structural block types

Since StreamField accepts an instance of StreamBlock as a parameter, in place of a list of block types, this makes it possible to re-use a common set of block types without repeating definitions:

class HomePage(Page):
    carousel = StreamField(
        CarouselBlock(max_num=10, block_counts={'video': {'max_num': 2}}),
        use_json_field=True
    )

StreamBlock accepts the following additional options as either keyword arguments or Meta properties:

body = StreamField([
    # ...
    ('event_promotions', blocks.StreamBlock([
        ('hashtag', blocks.CharBlock()),
        ('post_date', blocks.DateBlock()),
    ], form_classname='event-promotions')),
], use_json_field=True)
class EventPromotionsBlock(blocks.StreamBlock):
    hashtag = blocks.CharBlock()
    post_date = blocks.DateBlock()

    class Meta:
        form_classname = 'event-promotions'

© 2014-present Torchbox Ltd and individual contributors.
All rights are reserved.
Licensed under the BSD License.
https://docs.wagtail.org/en/stable/reference/streamfield/blocks.html