Add StreamField context to block examples

pull/6947/head
Matt Westcott 2021-03-23 18:23:51 +00:00 zatwierdzone przez Matt Westcott
rodzic 9f084fff6d
commit 3e2c928e2f
1 zmienionych plików z 42 dodań i 27 usunięć

Wyświetl plik

@ -373,12 +373,15 @@ Structural block types
.. code-block:: python .. code-block:: python
body = StreamField([
# ...
('person', blocks.StructBlock([ ('person', blocks.StructBlock([
('first_name', blocks.CharBlock()), ('first_name', blocks.CharBlock()),
('surname', blocks.CharBlock()), ('surname', blocks.CharBlock()),
('photo', ImageChooserBlock(required=False)), ('photo', ImageChooserBlock(required=False)),
('biography', blocks.RichTextBlock()), ('biography', blocks.RichTextBlock()),
], icon='user')) ], icon='user')),
])
Alternatively, StructBlock can be subclassed to specify a reusable set of sub-blocks: Alternatively, StructBlock can be subclassed to specify a reusable set of sub-blocks:
@ -421,17 +424,23 @@ Structural block types
.. code-block:: python .. code-block:: python
('ingredients_list', blocks.ListBlock(blocks.CharBlock(label="Ingredient"))) body = StreamField([
# ...
('ingredients_list', blocks.ListBlock(blocks.CharBlock(label="Ingredient"))),
])
Any block type is valid as the sub-block type, including structural types: Any block type is valid as the sub-block type, including structural types:
.. code-block:: python .. code-block:: python
body = StreamField([
# ...
('ingredients_list', blocks.ListBlock(blocks.StructBlock([ ('ingredients_list', blocks.ListBlock(blocks.StructBlock([
('ingredient', blocks.CharBlock()), ('ingredient', blocks.CharBlock()),
('amount', blocks.CharBlock(required=False)), ('amount', blocks.CharBlock(required=False)),
]))) ]))),
])
The following additional option is available as either a keyword argument or a Meta class attribute: The following additional option is available as either a keyword argument or a Meta class attribute:
@ -444,6 +453,8 @@ Structural block types
.. code-block:: python .. code-block:: python
body = StreamField([
# ...
('carousel', blocks.StreamBlock( ('carousel', blocks.StreamBlock(
[ [
('image', ImageChooserBlock()), ('image', ImageChooserBlock()),
@ -454,7 +465,8 @@ Structural block types
('video', EmbedBlock()), ('video', EmbedBlock()),
], ],
icon='cogs' icon='cogs'
)) )),
])
As with StructBlock, the list of sub-blocks can also be provided as a subclass of StreamBlock: As with StructBlock, the list of sub-blocks can also be provided as a subclass of StreamBlock:
@ -490,12 +502,15 @@ Structural block types
:param form_classname: An HTML ``class`` attribute to set on the root element of this block as displayed in the editing interface. :param form_classname: An HTML ``class`` attribute to set on the root element of this block as displayed in the editing interface.
.. code-block:: python .. code-block:: python
:emphasize-lines: 4 :emphasize-lines: 6
body = StreamField([
# ...
('event_promotions', blocks.StreamBlock([ ('event_promotions', blocks.StreamBlock([
('hashtag', blocks.CharBlock()), ('hashtag', blocks.CharBlock()),
('post_date', blocks.DateBlock()), ('post_date', blocks.DateBlock()),
], form_classname='event-promotions')) ], form_classname='event-promotions')),
])
.. code-block:: python .. code-block:: python
:emphasize-lines: 6 :emphasize-lines: 6