【问题标题】:Jinja2 if statement, where MySQL date type is nullJinja2 if 语句,其中 MySQL 日期类型为 null
【发布时间】:2020-09-22 16:25:55
【问题描述】:

我在从 MySQL 数据库查询数据的烧瓶应用程序中使用 jinja2 表示法生成一个表。

html模板:


{% for j in JRN %}
  <tr>
    <td>{{ j[0] }}</td>
    {% if j[1] != '0000-00-00' %}
        <td>{{ j[1] }}</td>
    {% else %}
        <td></td>
    {% endif %}
  ...
{% endfor %}

制作:

如果日期 j[1] 为空或“0000-00-00”,我希望表格为空白,而不是“无”。

Date 字段的类型为:date

除了上面显示的if子句之外,我也试过了:

...

{% elif j[1] != '0' %}
   <td>{{ j[1] }}</td>
{% elif j[1] != 0 %}
   <td>{{ j[1] }}</td>
{% elif j[1] != 'None' %}
   <td>{{ j[1] }}</td>
{% elif j[1] != 'NULL' %}
   <td>{{ j[1] }}</td>
{% else %}
   <td></td>
...

感谢您的帮助

【问题讨论】:

标签: html mysql jinja2


【解决方案1】:

您为什么不尝试使用 HTML 实体将该区域保持空白 更新代码:

{% for j in JRN %}
  <tr>
    <td>{{ j[0] }}</td>
    {% if j[1] != '0000-00-00' %}
        <td>{{ j[1] }}</td>
    {% endif %}
    {% if j[1] == '0000-00-00' %}
        <td> &nbsp; </td>
    {% endif %}
{% endfor %}

如果问题仍然存在,请告诉我。

【讨论】:

  • jinja2.exceptions.TemplateSyntaxError:遇到未知标签“endfor”。您可能犯了嵌套错误。 Jinja 期待这个标签,但目前正在寻找“endif”。需要关闭的最里面的块是'if'。
  • 我还在第一个 if 之后添加了 {% endif %},但它仍然没有摆脱 None
猜你喜欢
  • 2010-11-30
  • 2014-06-01
  • 2013-10-11
  • 2017-03-29
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多