Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - How to loop dictionary with multiple values in Jinja?

I have a dictionary like so: {'a': [Object, 0], 'b': [Object, 1] } Where object is an actual object with multiple attributes. I'm trying to a check on each key to see if the second value in the array is a 0 or 1. If it is a 1, then I'll display "Hello" if it's a 0, I'll display "Goodbye" Here's what I have so far that doesn't seem to work:

{% for key in follower_list %}
   {% if follower_list[key][1] == 0 %}
        <p>Hello</p>
   {% else %}
         <p>Goodbye</p>
{% endif %}

Here, follower_list is the dictionary. I'm getting an error by my IDE saying operator expected where follower_list[key][1] is there a way I can do this type of logic in Jinja?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using this method:

{% for key, value in follower_list.items() %}
    {% key %}
    {% value %}

This calls the items function on the dictionary letting you iterate through all the keys and values of the dictionary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...