2015-12-10 6 views

答えて

13

これは、StreamFieldRawHTMLBlockが有効になっていることを確認するための最も簡単な方法です。次のようにフィールドにコンテンツを追加するためのプロセスは次のとおりです。

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にそれを保存し、保存します。

+0

少なくともWagtail 1.5では、辞書の配列から作成されたJSON文字列を使用する必要はありません。代わりに次のようにタプルの配列を直接使うことができます: 'my_page.body = [( 'raw_html'、original_html)]' – CoreDumpError

関連する問題