Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RichText StreamFields
# demo/blocks.py; or sterams/blocks.py
"""Stream Blocks."""
from wagtail.core import blocks
class CustomRichTextBlock(blocks.StructBlock):
"""Rich text content."""
richtext_content = blocks.RichTextBlock(required=True)
bg_color = blocks.ChoiceBlock(required=True, choices=[
('white', 'White'),
('blue', 'Blue'),
('black', 'Black'),
], default='white')
class Meta:
"""Provide additional meta information."""
template = "streams/custom_richtext_block.html"
icon = "edit"
label = "Custom Richtext"
class CustomAloneRichTextBlock(blocks.RichTextBlock):
class Meta:
template = "streams/custom_alone_richtext_block.html"
icon = "edit"
label = "Custom [Alone] Richtext"
{# streams/custom_alone_richtext_block.html #}
<div style='padding: 50px;'>
{{ self }}
</div>
{# streams/custom_richtext_block.html #}
<div style='padding: 50px; background-color: {{ self.bg_color }}'>
{{ self.richtext_content }}
</div>
{# templates/demo/demo_page.html #}
{% extends 'base.html' %}
{% load wagtailcore_tags %}
{% block content %}
{% for block in page.content %}
{# Loop through every streamfield and include the rendered template files to your page. #}
{% include_block block %}
{% endfor %}
{% endblock content %}
"""blog/models.py Demo page."""
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from streams import blocks
# If pathing isn't a strong skill yet, you can use this instead:
# from . import blocks
class DemoPage(Page):
"""A demo page class."""
template = "demo/demo_page.html"
content = StreamField(
[
("custom_name", blocks.CustomRichTextBlock()),
("alone_richtext", blocks.CustomAloneRichTextBlock()),
],
null=True,
blank=True,
)
content_panels = [
FieldPanel("title", classname="full title"),
StreamFieldPanel("content"),
]
class Meta:
"""Meta information."""
verbose_name = "Demo Page"
verbose_name_plural = "Demo Pages"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment