I want to create a BlogPage programmatically in wagtail
with setting its StreamField value. I can set heading
field. But I'm getting AttributeError: 'unicode' object has no attribute 'source'
when I try to set the paragraph
field. I want to set an image too.
This is my BlogPage model.
models.py
class BlogPage(Page):
template = 'wagtail/test_page.html'
author = models.CharField(max_length=255)
date = models.DateField("Post date")
body = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
])
content_panels = Page.content_panels + [
FieldPanel('author'),
FieldPanel('date'),
StreamFieldPanel('body'),
]
this my code to create page by running this script.
create_page.py
new_image_page = BlogPage(
title='Blog',
slug='michael',
author='michael',
date='2017-12-13',
body=[('heading','New Heading'), ('heading','New Heading 23232'), ('paragraph','My Paragraph')]
)
directory_page = Page.objects.get(slug='home')
directory_page.add_child(instance=new_image_page)
revision = new_image_page.save_revision()
revision.publish()
new_image_page.save()