简体   繁体   中英

Jinja python for loop syntax

I have a jinja code for python and its giving me an error it doesn't give me in python

{% for i, juice in enumerate(a['juice'] for a in television):};
               alert({{ juice }});
            {% endfor %};

The Error I'm getting is

 expected token ',', got 'for'

You don't need to add : at the end of the for statements in Jinja2. And, you are not properly closing the tag - missing the % before the } .

Plus, there is no enumerate() function in Jinja2, use the loop.index0 :

{% for a in television %}
    {{ loop.index0 }}, {{ a["juice"] }}
{% endfor %}

If you want to use more Python in the templates, you should probably look at the Mako engine .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM