Created
July 21, 2018 23:18
-
-
Save KalobTaulien/098656ee1f706634fd69743304038229 to your computer and use it in GitHub Desktop.
Wagtail 2: Adding Streamfields to a Custom Page
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
"""Custom Page model(s).""" | |
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel, StreamFieldPanel | |
from wagtail.wagtailcore.fields import StreamField | |
from wagtail.wagtailcore.models import Page | |
from wagtail.wagtailsearch import index | |
from .streamfields import * # Replace * with the streamfields you want to use | |
class CustomPage(Page): | |
"""A custom page class.""" | |
template = 'templates/custom_page.html' | |
content = StreamField([ | |
('image_block', ImageBlock()), | |
('logo_parade', LogoParadeBlock()), | |
('content', RichTextBlock()), | |
], null=True) | |
search_fields = Page.search_fields + [ | |
index.SearchField('content'), | |
] | |
content_panels = [ | |
MultiFieldPanel([ | |
FieldPanel('title'), | |
FieldPanel('subtitle'), | |
], 'Banner'), | |
StreamFieldPanel('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
# -*- coding: utf-8 -*- | |
"""Streamfield Blocks.""" | |
from wagtail.core import blocks | |
from wagtail.images.blocks import ImageChooserBlock | |
from wagtail.snippets.blocks import SnippetChooserBlock | |
class ImageBlock(blocks.StructBlock): | |
"""An image, title, some content, """ | |
title = blocks.CharBlock(max_length=50, required=True) | |
image = ImageChooserBlock(required=True) | |
content = blocks.RichTextBlock(required=False) | |
button_text = blocks.CharBlock(required=False, default='Learn More',) | |
button_link = blocks.CharBlock(required=False) | |
members_only = blocks.BooleanBlock( | |
default=False, | |
required=False, | |
help_text='Select if this is a "Members Only" block', | |
) | |
class Meta: | |
"""Meta.""" | |
label = 'Image Block' | |
template = 'templates/streamfields/image.html' | |
icon = 'image' | |
class LogoParadeBlock(blocks.StructBlock): | |
"""Logo Parade.""" | |
title = blocks.CharBlock(max_length=50, required=True) | |
images = blocks.ListBlock(blocks.StructBlock([ | |
('image', ImageChooserBlock(required=True, help_text='Optimum dimensions should be: xxx by xxx')), | |
('link', blocks.CharBlock(required=False)), | |
('new_window', blocks.BooleanBlock(required=False, default=True, help_text='Check this option if you want links to open in a new tab')), | |
])) | |
class Meta: | |
"""Meta.""" | |
label = 'Logos' | |
icon = 'image' | |
template = 'templates/streamfields/logo_parade.html' | |
class RichTextBlock(blocks.RichTextBlock): | |
"""Rich text block.""" | |
class Meta: | |
"""Provide additional meta information.""" | |
template = 'templates/streamfields/richtext.html' | |
icon = 'edit' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment