How to avoid 1 as boolean value in django template (1 is taking as boolean value Ture)?

Viewed 118

My view is returning a dictionary.

**{'id': 1, 'user_info_id': 1, 'coding': False, 'testing': False, 'req_analysis': True}**

In Django template, I want to print all the keys of value == True. I have written the below code in Django template.

{% for key, value in db_data.0.items %}
    {% if value == True %} 
        {{ key }}
    {% endif %}  
{% endfor %}  

But, in output, I am getting key with value True as well as the keys with value 1.

O/P : 
id
user_info_id
testing

I want output as "testing" only.

Can anyone please help me with this.

1 Answers

You can make use of is operator [Django-doc] instead:

{% for key, value in db_data.0.items %}
    {% if value is True %} 
        {{ key }}
    {% endif %}  
{% endfor %}

is checks if two objects are the same object. Whereas == checks if two objects are equivalent. For example in Python 1 and True; and 0 and False are considered equivalent:

>>> 1 == True
True
>>> True == True
True
>>> 1 == 1
True
>>> True is True
True
>>> 1 is True
False
Related