Skip to content

Instantly share code, notes, and snippets.

@KalobTaulien
Created January 20, 2019 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KalobTaulien/5e6f4da28f786a61dac86ac31f5bfa99 to your computer and use it in GitHub Desktop.
Save KalobTaulien/5e6f4da28f786a61dac86ac31f5bfa99 to your computer and use it in GitHub Desktop.
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