Skip to content
Advertisement

How to break lines when using wagtail BlockQuoteBlock?

I have this model:

from wagtail.wagtailcore import blocks

class BlogPage(Page):
    date = models.DateField("Post date")
    intro = RichTextField(blank=True)
    body = StreamField([
        ('heading', blocks.CharBlock(classname="full title")),
        ('paragraph', blocks.RichTextBlock()),
        ('image', ImageChooserBlock()),
        ('code', CodeBlock()),
        ('markdown', MarkDownBlock()),
        ('media', TestMediaBlock(icon='media')),
        ('blockquote', blocks.BlockQuoteBlock())
    ])

When I’m saving page with some text using blockquote I use some line breakes and even <br> tags:

enter image description here

But on the page there are no line breaks after it:

enter image description here

So how to make this work and save line breaks? I’m using wagtail 1.13.1.

Advertisement

Answer

I think it was done because of security reasons. But It is possible to solve the problem – redefine BlockQuoteBlock for example like this:

from django.utils.safestring import mark_safe
from django.utils.html import format_html

from wagtail.wagtailcore import blocks


class BlockQuoteBlock(blocks.TextBlock):

    def render_basic(self, value, context=None):
        if value:
            return format_html(
                '<blockquote>{0}</blockquote>', mark_safe(value))
        else:
            return ''

    class Meta:
        icon = "openquote"

I’ve added mark_safe() function to the original implementation. And then use this block in the model, if you do so, then <br> tags begin to work

3 People found this is helpful
Advertisement