0

I am generating a table using the jinja2 notation, within a flask app querying data from a MySQL database.

The html template:


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

producing: enter image description here

Where Date, j[1], is null, or ‘0000-00-00’, I would like the table to be blank, rather than ‘None’.

The Date field is of type: date

In addition to the if clause shown above, I have tried:

...

{% 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>
...

Thanks for your help

tedioustortoise
  • 259
  • 3
  • 20
  • maybe this thread could help you https://stackoverflow.com/questions/19614027/jinja2-template-variable-if-none-object-set-a-default-value – cizario Jun 28 '20 at 11:09

1 Answers1

0

Why don't you try using HTML entities to keep that area blank updated code:

{% 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 %}

Let me know if the problem persists.

Madhav Parikh
  • 73
  • 2
  • 9
  • jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. You probably made a nesting mistake. Jinja is expecting this tag, but currently looking for 'endif'. The innermost block that needs to be closed is 'if'. – tedioustortoise Jun 05 '20 at 12:22
  • I also added {% endif %} after the first if, but it still didn't get rid of the None – tedioustortoise Jun 05 '20 at 12:23