3

I have a python Dictionary that looks like this

[{"hat": 91, "d1b": 2, "d1c": 1, "d1d": 5, "d1e": 7, "d1f": 77, "d1e": 999}
{"hat": 1, "d2b": 2, "d2c": 3, "d2d": 4, "d2e": 5, "d2f": 6, "d2e": 7}
{"hat": 1, "d3b": 2, "d3c": 3, "d3d": 4, "d3e": 5, "d3f": 6, "d3e": 7}]

And i pass this as a dictionary object (mydict) from python to jinja

What I'm tryrng to do is loop through each dictionary and print out the value of the key i search for. And have that show in a jquery alert box.

$(document).ready(function() {

        {% for i in mydict %}
          {{ loop.index0 }}, {{a,["hat"] }}
               alert( {{ hat }} );
            {% endfor %}
        });

When i go to my web page it gives me an error of

Uncaught SyntaxError: Unexpected token &

$(document).ready(function() {


          0, (Undefined, [[hat[])
               alert(  );

          1, (Undefined, [hat])
               alert(  );

          2, (Undefined, [hat])
               alert(  );

        });

Its not being defined, and its not printing an alert.

jumpman8947
  • 249
  • 3
  • 6
  • 13

1 Answers1

7

You need to use call like python for dictionary (it's not collection):

{% for i in dict %}
    {{ i['hat'] }}
{% endfor %}

Collections can be accessed as dictionaries, and dictionaries cannot be called as a collection. Any way you need use i.hat if this collection or i['hats'] if it's a collection or a dictionary.

Just try to replace it:

alert( {{ hat }} );

to:

alert( {{ i['hat'] }} );
JRazor
  • 2,707
  • 18
  • 27