kopia lustrzana https://github.com/wagtail/wagtail
Support overriding form classname and template within StructBlock
rodzic
7c8f14e545
commit
8bd6d65a20
|
@ -269,6 +269,24 @@ This defines ``PersonBlock()`` as a block type that can be re-used as many times
|
|||
])
|
||||
|
||||
|
||||
To customise the styling of the block as it appears in the page editor, your subclass can specify a ``form_classname`` attribute in ``Meta`` to override the default value of ``struct-block``:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
class PersonBlock(blocks.StructBlock):
|
||||
first_name = blocks.CharBlock(required=True)
|
||||
surname = blocks.CharBlock(required=True)
|
||||
photo = ImageChooserBlock()
|
||||
biography = blocks.RichTextBlock()
|
||||
|
||||
class Meta:
|
||||
icon = 'user'
|
||||
form_classname = 'person-block struct-block'
|
||||
|
||||
|
||||
You can then provide custom CSS for this block, targeted at the specified classname, by using the ``insert_editor_css`` hook (see :doc:`Hooks </reference/hooks>`). For more extensive customisations that require changes to the HTML markup as well, you can override the ``form_template`` attribute in ``Meta``.
|
||||
|
||||
|
||||
ListBlock
|
||||
~~~~~~~~~
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<div class="struct-block">
|
||||
<div class="{{ classname }}">
|
||||
{% if help_text %}
|
||||
<div class="object-help help">{{ help_text }}</div>
|
||||
{% endif %}
|
||||
<ul>
|
||||
{% for child in bound_child_blocks %}
|
||||
{% for child in children.values %}
|
||||
<li>
|
||||
{% if child.block.label %}
|
||||
<label{% if child.id_for_label %} for="{{ child.id_for_label }}"{% endif %}>{{ child.block.label }}</label>
|
||||
|
|
|
@ -23,6 +23,8 @@ class BaseStructBlock(Block):
|
|||
class Meta:
|
||||
default = {}
|
||||
template = "wagtailadmin/blocks/struct.html"
|
||||
form_classname = 'struct-block'
|
||||
form_template = 'wagtailadmin/block_forms/struct.html'
|
||||
|
||||
def __init__(self, local_blocks=None, **kwargs):
|
||||
self._constructor_kwargs = kwargs
|
||||
|
@ -72,15 +74,19 @@ class BaseStructBlock(Block):
|
|||
else:
|
||||
error_dict = {}
|
||||
|
||||
bound_child_blocks = [
|
||||
block.bind(value.get(name, block.get_default()), prefix="%s-%s" % (prefix, name),
|
||||
errors=error_dict.get(name))
|
||||
bound_child_blocks = collections.OrderedDict([
|
||||
(
|
||||
name,
|
||||
block.bind(value.get(name, block.get_default()),
|
||||
prefix="%s-%s" % (prefix, name), errors=error_dict.get(name))
|
||||
)
|
||||
for name, block in self.child_blocks.items()
|
||||
]
|
||||
])
|
||||
|
||||
return render_to_string('wagtailadmin/block_forms/struct.html', {
|
||||
'bound_child_blocks': bound_child_blocks,
|
||||
return render_to_string(self.meta.form_template, {
|
||||
'children': bound_child_blocks,
|
||||
'help_text': getattr(self.meta, 'help_text', None),
|
||||
'classname': self.meta.form_classname,
|
||||
})
|
||||
|
||||
def value_from_datadict(self, data, files, prefix):
|
||||
|
|
Ładowanie…
Reference in New Issue