Wagtail 2.2 中 StructBlock 的自定义渲染

Custom rendering of a StructBlock in Wagtail 2.2

我正在使用带有以下代码的简单 StructBlock:

from wagtail.core import blocks

class ProfileById(blocks.StructBlock):

  ids = blocks.CharBlock(label='Profile by ID')

  class Meta:
    template = 'myapp/blocks/profile_by_id.html'
    icon = 'user'

但是,当此组件呈现到页面时,它应该使用 f'https://api.server.ours/profiles/?ids={ids}&format=json' 之类的 url 对服务器执行快速 API 调用,然后将 json进入 Python 可以遍历的内容,然后在 myapp/blocks/profile_by_id.html 模板中利用该数据。

是否有一个 class 从 StructBlock 扩展的定义可以重写为“做事”,让我们将额外的数据放入模板上下文中,以便在模板被渲染时,额外的数据可以也被利用了吗?

我试图在 wagtail 文档中找到类似的内容,但什么也看不到,只有对于页面扩展,您可以覆盖 serve() def,这是一个完全不同的层次结构树。

块 类 定义一个 get_context 方法,可以重写该方法以将其他上下文变量插入模板:

http://docs.wagtail.io/en/v2.2.2/topics/streamfield.html#template-rendering (搜索 is_happening_today 以获取相关示例代码)