【问题标题】:django template if statementdjango 模板 if 语句
【发布时间】:2015-08-20 19:09:44
【问题描述】:

我有一个表格,我正在用模板中的 for 循环列表中的信息填充该表格。一旦达到特定值,我想结束当前表并创建一个新表。我写了这个打印表中的所有值但不输入 if 语句:

 <table class="table">
                {% for  x, y, z in defStats %}
                    <tr class = "info">

                        <td>
                            {{ x }}
                        </td>
                        <td>
                            {{ y }}
                        </td>
                        <td>
                            {{ z }}
                        </td>

                    </tr>
                    {% if x == " " %}
                        <p>HERE</p>
                        </table>
                        <table>
                    {% endif %}




                {% endfor %}
            </table>

我要查找的字符是一个空字符串。我什至尝试了一个 if 语句,它保证评估为 true,并且根本没有进入 if 语句:

 <table class="table">
                {% for  x, y, z in defStats %}
                    <tr class = "info">

                        <td>
                            {{ x }}
                        </td>
                        <td>
                            {{ y }}
                        </td>
                        <td>
                            {{ z }}
                        </td>

                    </tr>
                    {% if x == x %}
                        <p>HERE</p>
                        </table>
                        <table>
                    {% endif %}




                {% endfor %}
            </table>

在这两种情况下,根本就没有输入 if 语句。不知道怎么回事,谢谢。

【问题讨论】:

  • 在第二种情况下,您确定正在使用更新的模板吗?首先,空字符串是'',而不是' '(这是一个空格)。
  • 我无法弄清楚您希望采取的方法背后的原因,也许如果我们知道您的目标,我们可能会采用不同的方法,可能同样有帮助
  • 这是一个模板问题,我很抱歉刚刚得到它的工作

标签: django if-statement django-templates


【解决方案1】:

尝试替换:

{% if x == " " %}  
 with
{% if x == None %}

【讨论】:

  • 这不会测试空字符串。您使用isNone 进行测试,尽管这在这里甚至不相关。
猜你喜欢
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2017-07-20
  • 2015-05-14
  • 2021-06-30
  • 2020-08-09
  • 2014-11-21
相关资源
最近更新 更多