【问题标题】:In Wagtail streamfield template how to check if a field of a structblock inside a structblock is null?在 Wagtail 流场模板中,如何检查结构块内结构块的字段是否为空?
【发布时间】:2020-04-11 11:42:52
【问题描述】:

我有一个这样的结构块:

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】:

    一个空白的CharBlock 等于空字符串,它与null 不一样(而且无论如何,Python 的null 值是None,而不是Null)。您可以像在模板中测试其他空值一样进行测试:{% if value.header.text %}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      相关资源
      最近更新 更多