user14603676
user14603676

Reputation:

Using Stream Field in WagTail throws an error in block content

I followed this official tutorial for WagTail. Now I want to change my BlogPage body column from RichText to StreamField in models.py. I follow this manual to make it works.

I changed BlogPage class to look like this

class BlogPage(Page):
    date = models.DateField("Post date")
    intro = models.CharField(max_length=250)
    tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
    categories = ParentalManyToManyField('blog_app.BlogCategory', blank=True)

    body = StreamField([
        ('heading', blocks.CharBlock(form_classname="full title")),
        ('paragraph', blocks.RichTextBlock()),
        ('image', ImageChooserBlock()),
    ])


    def main_image(self):
        gallery_item = self.gallery_images.first()
        if gallery_item:
            return gallery_item.image
        else:
            return None

    search_fields = Page.search_fields + [
        index.SearchField('intro'),
        index.SearchField('body'),
    ]

    content_panels = Page.content_panels + [
        MultiFieldPanel([
            FieldPanel('date'),
            FieldPanel('tags'),
            FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
        ], heading="Blog information"),
        FieldPanel('intro'),
        StreamFieldPanel('body', classname="full"),
        InlinePanel('gallery_images', label="Gallery images"),
    ]

Now I'm able to edit StreamField in Admin page of Wegtail.

I changed content of base.html from

{% block content %}{% endblock %}

to

{% include_block page.body %} to not throwing an error.

Now I'm able to see content of StreamField in my post. But because I replaced {% block content %} I'm not able to see anything else (like all posts on blog_index_page.html). But because blog_index_page is looping posts I have to include {% include_block page.body %} in base to properly render content of StramField and not throwing an error that the block content expected string.

My architecture is same as the tutorial site.

Error message is:

'richtext' template filter received an invalid value; expected string, got <class 'wagtail.core.blocks.stream_block.StreamValue'>.

Upvotes: 1

Views: 1109

Answers (1)

gasman
gasman

Reputation: 25227

Don't replace the {% block content %}{% endblock %} line in base.html. This is the base template shared by all page types, so it should only contain things that are meaningful to all page types, like the <title> tag - the {% block content %} is a placeholder that the individual templates (blog_index_page.html and blog_page.html) will use to insert their own content. See Template inheritance for more explanation.

The correct lines to change are:

  • in blog_page.html: {{ page.body|richtext }} becomes {% include_block page.body %}
  • in blog_index_page.html: {{ post.specific.body|richtext }} becomes {% include_block post.specific.body %}

Upvotes: 1

Related Questions