In this lesson we are going to learn how to add a basic StreamField to a a generic Wagtail CMS Page. We'll create a new app from scratch, and this StreamField will have a title and text (using StructBlock), a custom template, and it will lay the foundation for the next lesson which covers inheriting RichTextBlock and modifying the features it can have.
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.