Django template iterating through two dictionaries/variables

260 views
Skip to first unread message

Aaron Weisberg

unread,
Aug 26, 2016, 9:50:00 AM8/26/16
to Django users
I didn't really know how to label this question, but I think it's an interesting concept and I haven't seen a resolution anywhere else.

I currently have two different dictionaries as context for a template:

Enter code here...context ={
                             
'person':person,
                             
'scores':scores,
                           
}


Each of these dictionaries has a common key (person.Number), (scores.Number), however the person dictionary is based off of a model in my django database, while scores comes from a json feed that I parse through:

I currently run through a table in a template that looks like this:

Enter code here...<table class="table">
  <thead>
  <tr>
  <th>Person</th>
<th>Person Number</th>
<th>First Name</th>
<th>Last Name </th>
<th> City </th>
<th> Score</th>
  </tr>
  </thead>
  <tbody>
                       {% for person in persons %}
                                      <tr>
                                      <td>{{ person.Number }}</td>
                                      <td>{{ person.firstName }}</td>
                                                                      <td>{{ person.lastName }}</td>
                                                                      <td>{{ person.city }}</td>
                                                                      <td>??'''scores.score1 goes here'''</td>
 
                     {% endfor %}
   
  </table>

What I'm trying to figure out to do is figure out how to get that final detail loaded into the table where we would find the score specific to that person.Number.  In other words how to go through my scores dictionary in this template and find the corresponding score1 using the person.Number from the other dictionary as the value.


Let me know if you have any ideas.

Thanks.


ludovic coues

unread,
Aug 26, 2016, 11:41:15 AM8/26/16
to django...@googlegroups.com
You could write a filter [1]. It would be used like {{
scores|from_player:person }} and look like that:

def from_player (scores, player):
return scores[player.Number]

You might want to adjust to your actual code, check that you got the
right arguments and that kind of things.

[1] https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/d05c8fc4-706b-422b-bc11-b656f1950dea%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Aaron Weisberg

unread,
Aug 29, 2016, 10:51:08 AM8/29/16
to django...@googlegroups.com
Thanks ludovic,

I'm trying to write that python code presently- would I have to also change the context?  how would that look?

Thanks


> To post to this group, send email to django...@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/d05c8fc4-706b-422b-bc11-b656f1950dea%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/UlgghbsoqYI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Asad Jibran Ahmed

unread,
Aug 29, 2016, 11:53:54 AM8/29/16
to django...@googlegroups.com

No need to change the context, since the variable names the example here uses are the same as your existing context. It should work as long as you remember to {% load APPNAME %} your filters in the template first.

Asad Jibran Ahmed

unread,
Aug 29, 2016, 11:55:42 AM8/29/16
to Django users
Since the example given by Ludovic uses the same variable names that are already present in your context, you won't need to change anything in the context dictionary. Just use 
{% load APP_NAME %}
on top of the template file to load the filter and you should be good to go.

> To post to this group, send email to django...@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/d05c8fc4-706b-422b-bc11-b656f1950dea%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/UlgghbsoqYI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Aaron Weisberg

unread,
Aug 29, 2016, 12:12:56 PM8/29/16
to django...@googlegroups.com

Thanks Asad. Is that why I'm getting errors saying invalid filter name?

Also I'm having a difficult time thinking my way through the workflow. I'm defining a new function that requires scores(my URL based dictionary) and person (my database dictionary). The function then asks to return the scores of the corresponding game number. Then the template is looking for the correct value in the scores dictionary that has the same Number identification as the existing person.Number?

I think what is confusing me is the use of "player" in ludovics example

Thanks

Aaron


Asad Jibran Ahmed

unread,
Aug 29, 2016, 12:33:38 PM8/29/16
to Django users
No problem. The most probable reason why you're getting the invalid filter error is because you forgot to load the filters.

Your explanation of the workflow is correct, with one small correction. The template will handoff the search for the correct Score to the filter function. Whatever the filter function returns will then be inserted in place of the {scores|from_player:person} part in your template. One way of getting the correct score would be to loop over the items in your scores dictionary, and find the one where the identification numbers match. One suggestion, instead of returning a dictionary from your filter function, you should return the value you want to display. So maybe your filter can look something like:
def get_score_for_person(scores, person):
 
for s in scores:
 
if s['Number'] == p.Number:
   
return s['Score']


And you're right, maybe you can rename the filter to be get_score_for_person if that is more clear for your application.

> To post to this group, send email to django...@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/d05c8fc4-706b-422b-bc11-b656f1950dea%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/UlgghbsoqYI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

--
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...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/UlgghbsoqYI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

Aaron Weisberg

unread,
Aug 29, 2016, 12:39:52 PM8/29/16
to django...@googlegroups.com

I hate to be thick but could you explain to me how to load the filters and also how the template should look with the updated function?  Solving this will unlock a ton of functionality for my app.

Thanks,

Aaron


To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Asad Jibran Ahmed

unread,
Aug 29, 2016, 10:07:09 PM8/29/16
to django...@googlegroups.com
Unfortunately without knowing your project layout I can't show you exactly how to load the filters, but you should give this a read: https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/.

Your template should look like the example from Ludovic.
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Nate Granatir

unread,
Aug 30, 2016, 12:28:37 PM8/30/16
to Django users
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:

Aaron Weisberg

unread,
Aug 30, 2016, 10:41:29 PM8/30/16
to Django users
This is why i ask this group.

Everyone helped out, but  Nate's link pushed me in the right direction.

Sometimes I don't even know what to ask stack overflow, but the google group always helps out.

Thanks
Reply all
Reply to author
Forward
0 new messages