Leonardo
Leonardo

Reputation: 1

Jinja2 - Problem while looping a dictionary that uses hexadecimal numbers as key

I'm trying to loop over the following python dictionary using Jinja2 a 'for loop'

eg. {'0x1007c': '1'}. 

However Jinja complains about the hex value. Any ideas how can I fix it ?

I tried to escape the value but I'm not sure if this is the right way to fix the problem.

{% extends 'layout.html' %}

{% block body %}
    <h1>Devices</h1>
    <ul class="list.group">
        {% for device in devices %}
            <li class="list.group">{{ device.0x1007c }}</li> 
        {% endfor %}
    </ul>
{% endblock %}

The error message that I'm receiving is this:

jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'x1007c'

I was expecting retrieve the value 1.

Upvotes: 0

Views: 184

Answers (1)

Tianbo Ji
Tianbo Ji

Reputation: 125

If devices is a list and its each element device is a dict, then it should be {{device['0x1007c']}}.

If devices is dict, then device is the key, so it should be {{devices[device]}}.

Upvotes: 0

Related Questions