Installation and Usage¶
To install use pip:
pip install wagtailaltstreamfield
Add “altstreamfield” to your Django Project’s INSTALLED_APPS list.
Create some custom blocks and a django.db.Model subclass like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #filename: [yourapp]/models.py
from django.db import models
from wagtail.core.models import Page
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
from altstreamfield.edit_handlers import AltStreamFieldPanel
from altstreamfield.fields import AltStreamField
from altstreamfield.blocks import (
StreamBlock,
StructBlock,
BooleanField,
CharField,
ChoiceField,
DocumentChooserField,
ImageChooserField,
IntegerField,
RichTextField,
StreamBlockField,
TextField,
)
# Create some custom blocks
HEADING_TYPE_CHOICES = (
('h1', 'H1'),
('h2', 'H2'),
('h3', 'H3'),
('h4', 'H4'),
('h5', 'H5'),
('h6', 'H6'),
)
class Heading(StructBlock):
heading_type = ChoiceField(choices=HEADING_TYPE_CHOICES, required=True)
text = CharField()
class Meta:
icon = 'title'
class Paragraph(StructBlock):
text = RichTextField()
class Meta:
icon = 'pilcrow'
class DocumentLink(StructBlock):
title = CharField()
document = DocumentChooserField()
class Meta:
icon = 'doc-empty'
class SimpleStreamBlock(StreamBlock):
heading = Heading()
paragraph = Paragraph()
document = DocumentLink()
# Create your models here.
class HomePage(Page):
body = AltStreamField(SimpleStreamBlock)
content_panels = Page.content_panels + [
AltStreamFieldPanel('body', classname='full'),
]
|
If you are creating a large number of blocks it is a good idea to separate the blocks into a separate module or modules.