Django Field to Handle Image or Video
March 18, 2022
Short post covering a topic I had to spend some time figuring out. Using the Wagtail StreamField
, I created
a block for either an image or a video.
feedback_media = StreamField(
blocks.StreamBlock([
('image', ImageBlock()),
('video', blocks.RawHTMLBlock())
], max_num=1))
The ImageBlock
is a custom Block that includes extra data needed about the image.
class ImageBlock(StructBlock):
image = ImageChooserBlock(required=False)
alt_text = blocks.CharBlock(required=False)
link = blocks.URLBlock(required=False)
alignment = ImageFormatChoiceBlock()
identifier = blocks.CharBlock(required=False)
The RawHTMLBlock is from wagtail.core.blocks
and can take the embed HTML for a video.