1

我有一个问题,我在 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

4

1 回答 1

5

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

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

于 2017-02-07T10:55:31.103 回答