Created
January 20, 2019 23:02
-
-
Save KalobTaulien/5e6f4da28f786a61dac86ac31f5bfa99 to your computer and use it in GitHub Desktop.
RichText StreamFields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# streams/custom_alone_richtext_block.html #} | |
<div style='padding: 50px;'> | |
{{ self }} | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# streams/custom_richtext_block.html #} | |
<div style='padding: 50px; background-color: {{ self.bg_color }}'> | |
{{ self.richtext_content }} | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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