如何检查w块是否包含给定的文本

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

我有包含的ag / Django模板

{% for block in page.content %}
    {% include_block block %}
    {% if "Email address:" in block %}
        This is an email address
    {% endif %}
{% endfor %}

该块确实包含文本,但是if子句不返回True

怎么了?

django wagtail
2个回答
0
投票

问题是在块类型中。

此代码将帮助:

{% for block in page.content %}
    {% include_block block %}
    {% if "Email address:" in block|striptags %}
        This is an email address
    {% endif %}
{% endfor %}

0
投票

{% if "Email address:" in block %}应为:{% if "Email address:" in block.value %}

您从page.content循环获得的块对象不是一个简单的字符串-它包含block_type之类的属性以及允许将其呈现为HTML的方法-结果,像in之类的字符串比较将不会t直接在该对象上工作。

根据所定义的块类型,您可能还需要执行{% if block.block_type == 'text' %}这样的检查,以确保您没有对基于非字符串的块(例如StructBlock)进行字符串比较。

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