Rendering a dictionary in Jinja2

Your url_list should look like this:

url_list = [{'target': 'http://10.58.48.103:5000/', 'clicks': '1'}, 
            {'target': 'http://slash.org', 'clicks': '4'},
            {'target': 'http://10.58.48.58:5000/', 'clicks': '1'},
            {'target': 'http://de.com/a', 'clicks': '0'}]

Then using:

<li>{{ item["target"] }}</li> 

in your template will work.

Edit 1:

Your template think you’re passing a list in, so are you sure you’re passing in your original dict and not my above list?

Also you need to access both a key and a value in your dictionary (when you’re passing a dictionary rather than a list):

Python 2.7

{% for key, value in url_list.iteritems() %}
    <li>{{ value["target"] }}</li> 
{% endfor %}

Python 3

{% for key, value in url_list.items() %}
    <li>{{ value["target"] }}</li> 
{% endfor %}

Leave a Comment