Check if key exists in a Python dict in Jinja2 templates

84,743

Solution 1

Like Mihai and karelv have noted, this works:

{% if 'blabla' in item %}
  ...
{% endif %}

I get a 'dict object' has no attribute 'blabla' if I use {% if item.blabla %} and item does not contain a blabla key

Solution 2

You can test for key definition this way:

{% if settings.property is defined %}

#...
{% endif %}

Solution 3

This works fine doesn't work in cases involving dictionaries. In those cases, please see the answer by tshalif. Otherwise, with SaltStack (for example), you will get this error:

Unable to manage file: Jinja variable 'dict object' has no attribute '[attributeName]'

if you use this approach:

{% if settings.myProperty %}

note:
Will also skip, if settings.myProperty exists, but is evaluated as False (e.g. settings.myProperty = 0).

Share:
84,743
Amal Antony
Author by

Amal Antony

Updated on July 29, 2022

Comments

  • Amal Antony
    Amal Antony almost 2 years

    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?

  • Amal Antony
    Amal Antony over 9 years
    Can myProperty be a variable in the approach above?
  • Mihai Zamfir
    Mihai Zamfir over 9 years
    Yes, a key in a dictionary. Is the equivalent of hasKey. And btw, hasKey is deprecated. Use in instead
  • karelv
    karelv over 8 years
    when setting.myProperty exists, but it is equal to zero (integer 0) or to the boolean value False, this if test not for 'hasKey'. This means you need: {% if 'myProperty' in settings %}
  • ryantuck
    ryantuck about 8 years
    this doesn't work for me (at least working with ansible). i found i needed {% if settings.myProperty is defined %}.
  • chesterbr
    chesterbr over 7 years
    Same for me. In my particular case I'm using Jinja2 inside Ansible.
  • Florian Neumann
    Florian Neumann over 6 years
    I don't get why this is accepted as answer - it throws an error if the dict is missing the key. Please correct the answer.
  • Florian Neumann
    Florian Neumann over 6 years
    @MihaiZamfir There is no reason to interpret my commentary with excessive emotion. tshalif prooved the failure - and his answer seems to be the more robust one and even if "the" answer is right for a subset of use cases, it should in my opinion at least reflect that it might not fit for all use cases.
  • Edward Fung
    Edward Fung over 6 years
    Looks like this answer fails if settings.myProperty is an empty dictionary/list.
  • Drew
    Drew about 6 years
    Using jinja2 inside Ansible. Nothing helps. Both {% if 'blabla' in item %} and {% if item.blabla %} return the same 'dict object' has no attribute 'blabla' wtf???
  • Marc Tamsky
    Marc Tamsky over 5 years
    Answer by @RyanTuck is the most correct and works regardless of the Boolean (True/False) status of myProperty :: {% if settings.myProperty is defined %}
  • ma3oun
    ma3oun over 5 years
    fails when the key is not existent
  • blkpingu
    blkpingu over 4 years
    argument of type 'StringField' is not iterable. Does not work for me
  • Mayank Porwal
    Mayank Porwal almost 4 years
    This looks like the correct way of checking if a key is present in dict or not. +1
  • Samha'
    Samha' about 3 years
    Your answer checks for object attributes, not dict keys.