在Wagtail流场模板中,如何检查structblock内部的structblock的字段是否为空?

问题描述 投票:0回答:1

我有一个这样的StructBlock:

class CardBlock(blocks.StructBlock):
header= blocks.StructBlock([
    ("text", blocks.CharBlock(required=False, help_text="Header text")),
    ("classes", blocks.CharBlock(required=False, help_text="Header css classes")),
    ],
    template="streams/card_header_block.html")

image= ImageChooserBlock(required=False)
icon= blocks.CharBlock(required=False, help_text="fontawesome classes for an icon")

title= blocks.StructBlock([
    ("text", blocks.CharBlock(required=False, help_text="Title text")),
    ("classes", blocks.CharBlock(required=False, help_text="Title css classes")),
    ],
    template="streams/card_title_block.html")

bodyHTML = blocks.RawHTMLBlock()

footer= blocks.StructBlock([
    ("text", blocks.CharBlock(required=False, help_text="Footer text")),
    ("classes", blocks.CharBlock(required=False, help_text="Footer css classes")),
    ],
    template="streams/card_footer_block.html")

class Meta:
    template = "streams/card_block.html"
    icon = "placeholder"
    label = "Card"

以及这样的模板:

{% load wagtailcore_tags %}
{% load wagtailimages_tags %}

{% image value.image fill-300x150 as img %}
<div class="card {% if value.classes %} {{value.classes}} {% endif %}">
    {% if value.header.text is not Null %}
        {% include_block value.header %}
    {% endif %}

    {% if value.image %}
    <img src="{{ img.url }}" alt="{{ img.alt}}" class="card-img-top" />
    {% endif %}

    <div class="card-body">
        {% if value.title %}
            {% include_block value.title%}
        {% endif %}

        {% if value.subtitle %}
            {% include_block value.subtitle%}
        {% endif %}

        <div class="card-text">{% include_block value.bodyHTML %}</div>

        {% if value.link %}
            {% include_block value.link%}
        {% endif %}
    </div>
    {% if value.footer %}
        {% include_block value.footer%}
    {% endif%}
</div>

我正在尝试检查是否是子块,例如Header是否由页面编辑器填充了它的值。如果没有,我不显示HTML。但是我恐怕标题div仍然会出现。我提出条件的方式有些错误。

wagtail wagtail-streamfield
1个回答
0
投票

空白CharBlock等于空字符串,它不等于null(在任何情况下,Python的null值为None,而不是Null)。您可以使用与测试模板中其他空值相同的方法进行测试:{% if value.header.text %}

© www.soinside.com 2019 - 2024. All rights reserved.