Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new entry into a dictionary object while using jinja2?

I am not able to append add a new entry into a dictionary object while using jinja2 template.

For example, here I am using jinja2 template and I have created a data variable which is a dictionary. And after checking some if condition I WANT to append location attribute to the data object e.g.

{%- set data = {
                  'name' : node.Name,
                  'id' : node.id,
               }
-%}

{% if node.location !="" %}
    data.append({'location': node.location}) 
{% endif %}

However I could not find a way to achieve this and am getting the UndefinedError:

jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'append'

Has anyone faced this issue or could provide a reference to solve this?

I searched the web but could not find a solution i.e. how to achieve adding an entry to the dict object in the Jinja.

I have referred following and other web resources:

  1. http://cewing.github.io/training.codefellows/assignments/day22/jinja2_walkthrough.html
  2. In Jinja2 whats the easiest way to set all the keys to be the values of a dictionary?
  3. https://github.com/saltstack/salt/issues/27494
like image 606
hemant_maverik Avatar asked Apr 27 '16 10:04

hemant_maverik


People also ask

How do you write a for loop in Jinja2?

Jinja2 being a templating language has no need for wide choice of loop types so we only get for loop. For loops start with {% for my_item in my_collection %} and end with {% endfor %} . This is very similar to how you'd loop over an iterable in Python.

Does Jinja2 work with Python 3?

Jinja2 works with Python 2.6. x, 2.7. x and >= 3.3. If you are using Python 3.2 you can use an older release of Jinja2 (2.6) as support for Python 3.2 was dropped in Jinja2 version 2.7.


4 Answers

Without the jinja2.ext.do extension, you can do this:

{% set x=my_dict.__setitem__("key", "value") %}

Disregard the x variable and use the dictionary which is now updated.

UPD: Also, this works for len() (__len__()), str() (__str__()), repr() (__repr__()) and many similar things.

like image 173
Арсений Пичугин Avatar answered Oct 17 '22 01:10

Арсений Пичугин


Dictionaries do not have the append method. You can add a key-value pair like this though:

{% do data['location']=node.location %} 

or

{% do data.update({'location': node.location}) %}
like image 40
alpert Avatar answered Oct 17 '22 01:10

alpert


Key takeaways:

  1. dictionary does not support append().
  2. You can add the new item to the data dictionary by using {% do ... %} tag as shown here:

    {% do data.update({'location': node.location}) %}
    
  3. However, for the "do" tag to work properly you need to add the jinja2.ext.do extension to your jinja Environment.

like image 10
hemant_maverik Avatar answered Oct 16 '22 23:10

hemant_maverik


Without the do extension:

{%- set _ = dict.update({c.name: c}) -%}

Works in base Jinja2 on Python 3, where the __setitem__ solutions give me:

access to attribute '__setitem__' of 'dict' object is unsafe
like image 9
dsz Avatar answered Oct 17 '22 00:10

dsz