在 Wagtail streamfield 模板中如何检查结构块内结构块的字段是否为空?
In Wagtail streamfield template how to check if a field of a structblock inside a structblock is null?
我有一个像这样的结构块:
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 这样的 child 块是否由页面编辑器填充了它的值。如果不是我就不显示了HTML。但恐怕 header div 仍然会出现。我提出条件的方式有些不对。
一个空格CharBlock
等于空字符串,它和null不一样(而且无论如何,Python的null值是None
,不是Null
).您可以像在模板中测试其他空值一样进行测试:{% if value.header.text %}
我有一个像这样的结构块:
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 这样的 child 块是否由页面编辑器填充了它的值。如果不是我就不显示了HTML。但恐怕 header div 仍然会出现。我提出条件的方式有些不对。
一个空格CharBlock
等于空字符串,它和null不一样(而且无论如何,Python的null值是None
,不是Null
).您可以像在模板中测试其他空值一样进行测试:{% if value.header.text %}