Wigtail将设置添加到流块的正确方法

时间:2017-10-30 17:34:32

标签: django wagtail

说我们有一个流程块:

class CarouselStreamBlock(StreamBlock):

    image = StructBlock([
        ('file', ImageChooserBlock()),
        ('caption', RichTextBlock( classname='caption'  )),

    ])

我们有一个场景,我们希望管理编辑能够设置每个轮播的宽度,高度,bgcolor等...

我尝试将该设置添加为另一个名为carousel本身的结构块,其max_length为1.

  class CarouselStreamBlock(StreamBlock):
     settings = StructBlock([
                ('align', blocks.ChoiceBlock(choices=[('center', 'Center'), ('right', 'Right'), ('left', 'Left'), ], default='center', max_length=10) ),
                ('width', blocks.IntegerBlock(required=False ) ),
                ('background_color', blocks.CharBlock(max_length=10, required=False))
            ], icon='cog' )

    image = StructBlock([
                ('file', ImageChooserBlock()),
                ('caption', RichTextBlock( classname='caption'  )),

            ])

但我不满意这种方式。有没有更好的方法呢?

1 个答案:

答案 0 :(得分:2)

StreamBlock由一系列不同类型的子块组成。换句话说,当真正的设置应该是图像块或轮播块本身的一部分时,所有块都将成为彼此的兄弟姐妹。

如果每张图片都需要自己的设置,您可以执行以下操作:

class CustomImage(blocks.StructBlock):
    file = ImageChooserBlock()
    caption = RichTextBlock(classname='caption')

    align = blocks.ChoiceBlock(
        choices=[('center', 'Center'), ('right', 'Right'), ('left', 'Left')],
        default='center',
        max_length=10,
    )
    width = blocks.IntegerBlock(required=False)
    background_color = blocks.CharBlock(max_length=10, required=False)

class CarouselStreamBlock(blocks.StreamBlock):
    image = CustomImage()

class MyPage(Page):
    carousel = StreamField(CarouselStreamBlock())

但是,如果所有图像之间共享设置,则可以执行以下操作:

class CustomImage(blocks.StructBlock):
    file = ImageChooserBlock()
    caption = RichTextBlock(classname='caption')

class CarouselStreamBlock(blocks.StructBlock):
    align = blocks.ChoiceBlock(
        choices=[('center', 'Center'), ('right', 'Right'), ('left', 'Left')],
        default='center',
        max_length=10,
    )
    width = blocks.IntegerBlock(required=False)
    background_color = blocks.CharBlock(max_length=10, required=False)

    images = blocks.ListBlock(CustomImage())

class MyPage(Page):
    carousel = StreamField(CarouselStreamBlock())