#1
by
Hauke
Hi!
I created some sort of a nested dict, that O pass to the template using vars_for_template. Within the template, I would like to loop over these items to eventually create a twitter-ish feed. Unfortunately, it doesn't work too well. I am sure the answer is quite obvious but I don't see it...
So here is my Page:
class Results(Page):
@staticmethod
def vars_for_template(player: Player):
tweets = {'1':
{'handle': '@elhotzo',
'name': 'El Hotzo',
'date': '9. Nov.',
'text': 'Some Text A',
'replies': 9,
'retweets': 17,
'likes': 9
},
'2':
{'handle': '@aurelmertz',
'name': 'Aurel',
'date': '10. Nov.',
'text': 'Some Text B',
'replies': 4,
'retweets': 7,
'likes': 124
},
'3':
{'handle': '@aurelmertz',
'name': 'Aurel',
'date': '10. Nov.',
'text': 'Some Text C',
'replies': 17,
'retweets': 24,
'likes': 532
}
}
return dict(
tweets=tweets
)
The corresponding template:
{{ for i in tweets }}
<div class="card mb-5">
<div class="card-body">
<h5 class="card-title"></h5>
<h6 class="card-subtitle mb-2 text-muted">{{i.handle}} · {{i.date}}</h6>
<p class="card-text">
{{i.text}}
</p>
<i class="bi bi-chat text-secondary"> {{i.replies}} </i>
<i class="bi bi-arrow-repeat text-secondary"> {{i.retweets}} </i>
<i class="bi bi-heart text-secondary"> {{i.likes}} </i>
</div>
</div>
{{ endfor }}
Thanks a lot in advance!
#2
by
Chris_oTree
In Python, looping over a dict just gives you the keys, not the values. So the above code just yields '1', '2', '3'. Instead you should loop over tweets.items() or tweets.values().
#3
by
Hauke
Thanks once again for the very fast response! That works.