Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja 2 Templates: how I check in an if statement whether the boolean is False or None

Tags:

python

jinja2

So I want to show in a jinja2 template whether a state is True, False or None. Naturally I use a boolean since I have there the three states I need.

So I tried the following code to show the True and the False state and wanted to show nothing when it's None.

            {% if valid %}
            VALID
            {% elif not valid %}
            NOT VALID
            {%endif %}

But my problem is now that when valid is None it shows 'NOT VALID' How I can change that to showing nothing instead.

like image 854
muthan Avatar asked Mar 06 '15 04:03

muthan


1 Answers

Write if valid is none. Note that is in Jinja is not the same as Python's is. In this case is calls a Jinja filter named none. Here is the list of built-in filters.

jinja2.Template('{% if a is none %}None{% endif %}').render(a=None)
u'None'
jinja2.Template('{% if a is none %}None{% endif %}').render(a=False)
u''
like image 189
jd. Avatar answered Oct 29 '22 20:10

jd.