Tuesday, August 30, 2016

Re: Django template iterating through two dictionaries/variables

You could also just join the dicts before passing them to the template. It looks like it's relatively straightforward (scores is just a list that contains dicts of a 'Number' and a 'score', right?) Then you could turn scores into a dict, assuming Numbers are unique:

scores_new = {score['Number']:score['score'] for score in scores}

And then update the persons dict to include the score:
for person in persons:
    person.update({'score': scores_new.get(person['Number'])})

Then in your persons list in the template you'll just need to display person.score.

If you want to get really fancy (if, say, there's more than just a score you need to pull from the dicts in scores) you can see how to merge two lists of dicts on a common key here:
http://stackoverflow.com/questions/5501810/join-two-lists-of-dictionaries-on-a-single-key

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7bef3019-5c6b-4aae-abaf-c18e9d034d49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment