wagtail - 将片段注入(inject) Wagtail StreamField 接口(interface)

标签 wagtail wagtail-streamfield wagtail-snippet

我的公司正在使用 Wagtail 为我们的网站构建强大的页面,可能使用 StreamField 组件。我们想知道 Wagtail 是否允许我们创建可重用的部分(可能在片段中)并将它们注入(inject)页面的可能性。

我附上了一张简单的图表,说明我想做什么。请注意,虽然片段是一种可能的建议,但它不需要特定的解决方案。

enter image description here

当然,我们的目标是创建一个元素,它可以嵌入到另一个页面中,但可以在一个地方更新并在使用它的任何地方级联。

例如,Wordpress 有一个插件以短代码格式提供此功能:

[embed id=123456]

最佳答案

您可以为流字段创建新 block ,比方说MySnippetBlock 然后使用SnippetChooserBlock选择您想要的片段。

MySnippetBlock(StructBlock):
   title = CharBlock()
   snippet = SnippetChooserBlock()

然后在您的 StreamBlock 字段中,您可以使用上面的自定义 block :

MyPage(Page):
   stream_field_content = StreamField([('snippet_block', MySnippetBlock())])
...

或者,如果不需要附加附加信息,您可以直接在 StreamField 中使用 SnippetChooserBlock。

stream_field_content = StreamField([('snippet_block', SnippetChooserBlock())])

关于wagtail - 将片段注入(inject) Wagtail StreamField 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54655694/

相关文章:

python - Wagtail:如何使用 RichTextField 来表示模型的字符串?

django - TableBlock 如何指定表格 CSS 类

django-models - 在 Wagtail 中设置默认片段的最佳方法是什么

python - Streamfield 中的 Wagtail SnippetChooserBlock

django - 如何从wagtail block 模板中的struct_block.StructValue获取数据库id?

django - 添加新子页面时的 wagtail pathoverflow

python - 将 InlineField 添加到 ClusterableModel 不起作用

django - 在 Wagtail 管理界面中为 StreamField 提供默认值

django - 将类对象转换为 get_api_representation 函数的可读值