Django 在Wagtail编辑器界面中显示基于布尔块的附加内容面板

Django 在Wagtail编辑器界面中显示基于布尔块的附加内容面板,django,django-models,wagtail,Django,Django Models,Wagtail,我想将一个布尔块添加到我的Wagtail模型中,并仅在选中时显示内容面板字段。我已经了解了如何添加布尔块并根据其值在模板中呈现内容,但不知道如何使用它控制编辑器界面。这是我的模型。我只想在选中heldover布尔值时显示heldover_from,date chooser块 class AgendaPage(Page): author= models.CharField(max_length=255) date = models.DateField('Post date') agenda = S

我想将一个布尔块添加到我的Wagtail模型中,并仅在选中时显示内容面板字段。我已经了解了如何添加布尔块并根据其值在模板中呈现内容,但不知道如何使用它控制编辑器界面。这是我的模型。我只想在选中heldover布尔值时显示heldover_from,date chooser块

class AgendaPage(Page):
author= models.CharField(max_length=255)
date = models.DateField('Post date')
agenda = StreamField([
    ('agenda_item', blocks.StreamBlock([
        ('item_title', blocks.TextBlock()),
        ('item_text', blocks.TextBlock()),
        ('mtg_doc', blocks.StructBlock([
            ('doc_description', blocks.TextBlock()),
            ('doc_link', blocks.TextBlock()),
            ('submitted_late', blocks.BooleanBlock(required=False, help_text='Submitted Late')),
            ('heldover', blocks.BooleanBlock(required=False, help_text='Held Over')),
            ('heldover_from', blocks.DateBlock(required=False, help_text="Held Over From")

        ]))
      ]
    ))
])




content_panels = Page.content_panels + [
    FieldPanel('author'),
    FieldPanel('date'),
    StreamFieldPanel('agenda'),

]
(在我弄明白这一点后,我想知道我是否能满足要求,但前提是检查了heldover,而不是整个流程块)

{%for self.agenda%}
{%if block.block_type==“agenda_item”%}{{#将始终为真,但为了清楚起见,此处包含}
  • {block.value%中的子块的%s} {%if subblock.block_type==“item_title”%} {{subblock.value}} {%elif subblock.block_type==“项_文本”%} {{subblock.value}}

    {%elif subblock.block_type==“mtg_doc”%}
    {%ifequal subblock.value.submitted_late True%} (迟交) {%endifequal%}

    {%endif%} {%endfor%}
  • {%endif%} {%endfor%}
    您可以通过覆盖StructBlock的表单模板来实现这一点,如中所述-尽管它确实需要处理表单标记的一些相当低级的细节

    首先,让我们将
    mtg_doc
    块定义提取到它自己的类中,以获得更多的喘息空间:

    class MtgDocBlock(blocks.StructBlock):
        doc_description = blocks.TextBlock()
        doc_link = blocks.TextBlock()
        submitted_late = blocks.BooleanBlock(required=False, help_text='Submitted Late')
        heldover = blocks.BooleanBlock(required=False, help_text='Held Over')
        heldover_from = blocks.DateBlock(required=False, help_text="Held Over From")
    
        class Meta:
            form_template = 'myapp/block_forms/mtg_doc.html'
    
    
    class AgendaPage(Page):
        ...
        agenda = StreamField([
            ('agenda_item', blocks.StreamBlock([
                ('item_title', blocks.TextBlock()),
                ('item_text', blocks.TextBlock()),
                ('mtg_doc', MtgDocBlock())
            ])
        ])
    
    在这里,我向block类添加了一个
    form\u template
    参数,该参数指定了一个替代模板,用于代替Wagtail的内置模板呈现表单。因为我们不想更改实际的渲染,所以我们可以只包含内置模板(位于
    wagtailadmin/block\u forms/struct.html
    )并向其添加一些JS行为。在
    templates/myapp/block\u forms/mtg\u doc.html
    中:

    {% include "wagtailadmin/block_forms/struct.html" %}
    
    <script>
        // all fields of the block have a common prefix on the ID,
        // which is available as the template variable 'prefix'.
        // Retrieve the 'heldover' checkbox
        var checkbox = $('#{{ prefix }}-heldover');
    
        // Retrieve the 'li' element containing the 'heldover_from' field
        var field = $('#{{ prefix }}-alignment').closest('li');
    
        function showHideField() {
            // update the visibility of field according to the state of
            // the checkbox
            if (checkbox.is(':checked')) {
                field.show();
            } else {
                field.hide();
            }
        }
        // call showHideField immediately to reflect the initial state
        // of the checkbox
        showHideField();
        // trigger showHideField whenever the checkbox state is changed
        checkbox.change(showHideField);
    </script>
    
    {%include“wagtailadmin/block_forms/struct.html”%}
    //块的所有字段在ID上都有一个公共前缀,
    //可作为模板变量“prefix”使用。
    //检索“heldover”复选框
    var checkbox=$('{{prefix}}-heldover');
    //检索包含“heldover\u from”字段的“li”元素
    变量字段=$('#{{prefix}}-alignment')。最近('li');
    函数showHideField(){
    //根据字段的状态更新字段的可见性
    //复选框
    if(checkbox.is(':checked')){
    field.show();
    }否则{
    field.hide();
    }
    }
    //立即调用showHideField以反映初始状态
    //复选框的
    showHideField();
    //每当复选框状态更改时触发showHideField
    复选框。更改(showHideField);
    
    请显示您当前的模板代码好吗?我已经发布了当前的模板代码Gasman谢谢,我知道我想添加javascript来实现这一点,但我不确定在哪里/如何添加。我有一个noob问题:我在我的站点文件夹结构中没有看到wagtailadmin目录,你知道为什么会这样吗?因为它们位于wagtail核心级别,而不是应用程序/项目级别。这仍然是实现这一点的首选方法(wagtail 2.1)?谢谢你给我一个很棒的框架加斯曼!
    {% include "wagtailadmin/block_forms/struct.html" %}
    
    <script>
        // all fields of the block have a common prefix on the ID,
        // which is available as the template variable 'prefix'.
        // Retrieve the 'heldover' checkbox
        var checkbox = $('#{{ prefix }}-heldover');
    
        // Retrieve the 'li' element containing the 'heldover_from' field
        var field = $('#{{ prefix }}-alignment').closest('li');
    
        function showHideField() {
            // update the visibility of field according to the state of
            // the checkbox
            if (checkbox.is(':checked')) {
                field.show();
            } else {
                field.hide();
            }
        }
        // call showHideField immediately to reflect the initial state
        // of the checkbox
        showHideField();
        // trigger showHideField whenever the checkbox state is changed
        checkbox.change(showHideField);
    </script>