Amal Antony Amal Antony - 3 years ago 238
Python Question

Check if key exists in a Python dict in Jinja2 templates

I have a python dictionary:

settings = {
"foo" : "baz",
"hello" : "world"
}


This variable
settings
is then available in the Jinja2 template.

I want to check if a key
myProperty
exists in the
settings
dict within my template, and if so take some action:

{% if settings.hasKey(myProperty) %}
takeSomeAction();
{% endif %}


What is the equivalent of
hasKey
that I can use?

Answer Source

This works fine

{% if settings.myProperty %}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download