Ramona Daniela
Ramona Daniela

Reputation: 69

Correct way to pass a dictionary to the frontend using python

I am using python 2,6 and I am trying to send a dictionary to the frontend. I have a dictionary like this:

{"dict": {"1": 26, "0": 32, "3": 12, "2": 13, "4": 9}

This is how I am passing them to the front end:

data_load ={}
    c['dictio'] = {}
    dataload['dictio']['values'] = dict
    .....

And in the frond end:

<td colspan="2" class="{{ data['dictio']['values']}}">
                    <h4><b>Values</b></h4>
                    <h3>{{ data['dictio']['values']}}</h3>
                </td>   

The result is the list of values. How can I modify my code to get the values like this:

26
32
12
13
9

Upvotes: 2

Views: 1264

Answers (1)

PRMoureu
PRMoureu

Reputation: 13337

You could try the following pattern, with a for-loop to generate a tag h3 for each value (replace the name tag to handle another kind of section):

<td colspan="2">
    <h4><b>Values</b></h4>
    {% for value in data['dictio']['values'].itervalues() %}
        <h3>{{ value }}</h3>
    {% endfor %}
</td>  

The method itervalues is supported by the template engine to return a generator of the dictionary values used by the for-loop.

But don't use dict as variable name in your code.

Upvotes: 1

Related Questions