【问题标题】:StructBlock within StreamField is rendered as textStreamField 中的 StructBlock 呈现为文本
【发布时间】:2017-02-07 10:07:06
【问题描述】:

我有一个问题,我在 StreamField 中有一个 StrictBlock:

class DetailsTableBlock(StructBlock):
    title = CharBlock(required=False)
    description = RichTextBlock(required=False)
    table = TableBlock(template="home/blocks/table.html")

class MainStreamBlock(StreamBlock):
    ....
    table = DetailsTableBlock()

当我尝试使用以下方式渲染表格时出现问题:

{{ child.value.table }}

我得到的是:

{u'data': [[u'test', u'test', u'test'], [u'123', u'asd', u'asd'], [u'123', u'asd', u'asd']], u'first_row_is_table_header': True, u'first_col_is_header': False}

所以问题是如何在 StreamField 中使用 StructBlock 呈现 html?我正在使用 Wagtail 1.7

【问题讨论】:

    标签: wagtail wagtail-streamfield


    【解决方案1】:

    你应该使用:{{ child.value.bound_blocks.table }}

    完整的解释在wagtail documentation 给出,但简而言之:当你循环一个StreamField 的内容来输出它时,你有时会得到原始数据值,有时会得到一个BoundBlock 对象它知道值以及如何将其呈现为 HTML。当您访问 StructBlock 的子值时,您会获得原始值(因为这通常是在 StructBlock 模板中访问更有用的东西) - 要获得 BoundBlock 对象,您需要从 StructBlock 的 @987654325 中读取它改为@字典。

    【讨论】:

    • 再次感谢您的详细解释。 bound_blocks 就像一个魅力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    相关资源
    最近更新 更多