How to break lines when using wagtail BlockQuoteBlock?

为君一笑 提交于 2020-01-06 05:31:06

问题


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:

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

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


回答1:


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



来源:https://stackoverflow.com/questions/49179072/how-to-break-lines-when-using-wagtail-blockquoteblock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!