我正在从旧站点进行迁移,因此需要以编程方式将原始html添加到StreamField
Wagtail页面上。我该怎么做?
最简单的方法是确保RawHTMLBlock
已在上启用了该功能StreamField
,然后将其插入其中。向该字段添加内容的过程如下:
import json
original_html = '<p>Hello, world!</p>'
# First, convert the html to json, with the appropriate block type
raw_json = json.dumps([{'type': 'raw_html', 'value': original_html}])
# Load Wagtail page
my_page = Page.objects.get(id=1)
# Assuming the stream field is called 'body',
# add the json string to the field
my_page.body = raw_json
my_page.save()
您可以使用这种方法将其他类型的块添加到StreamField
-只需确保创建具有适当块类型的字典列表,然后将其转换为json并保存。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句