How to Add a Basic StreamField to your Wagtail CMS Page
Note: This is a 2 part video lesson. Part 2 will cover how to add RichText StreamFields and how to limit its features.
In this video we're going to explore what StreamFields are and how to add them to our page.
StreamFields are a complex data type in the sense that they are not simple columns in the database. An example of a generic StructBlock StreamField can be found below:
# streams/blocks.py from wagtail.core import blocks class TitleAndTextBlock(blocks.StructBlock): """Title and text and nothing else.""" title = blocks.CharBlock(required=True, help_text="Add your title") text = blocks.TextBlock(required=True, help_text="Add additional text") class Meta: # noqa template = "streams/title_and_text_block.html" icon = "edit" label = "Title & Text"
# flex/models.py # ..... from streams import blocks.py class FlexPage(Page): # .... content = StreamField( [ ("title_and_text", blocks.TitleAndTextBlock()), ], null=True, blank=True, )
The Git Commit
If you're interested in seeing the entire commit for this video (including part 2), all the source code is available here.
Was this helpful to you?
Sharing is caring. Help the community by sharing this article.