Can I use a calculated value for a key to a dictionary?

28 views
Skip to first unread message

Joel Goldstick

unread,
Sep 18, 2014, 2:08:27 PM9/18/14
to django...@googlegroups.com
I have some embed loops which iterate over league and division. I
want to print the teams in each. I obviously can't do what i tried in
line 19. Is there a way to calculate a key to a dictionary I want to
iterate over?

14{% for l in leagues %}
15 <h3>{{l}}</h3>
16 {% for d in divisions %}
17 <h4>{{d}}</h4>
18 <table>
19 {% for team in {{l}}{{d}} %}
20 <tr><td>{{team}}</td><td> {{ team.wins }}-</td><td>{{ team.losses
}}</td> </li>
21 {% endfor %}

Here is my context:

{u'ALW': [<Team: Oakland Athletics 1975>, <Team: Kansas City Royals
1975>, <Team: Texas Rangers 1975>, <Team: Minnesota Twins 1975>,
<Team: Chicago White Sox 1975>, <Team: California Angels 1975>],
u'ALE': [<Team: Boston Red Sox 1975>, <Team: Baltimore Orioles 1975>,
<Team: New York Yankees 1975>, <Team: Cleveland Indians 1975>, <Team:
Milwaukee Brewers 1975>, <Team: Detroit Tigers 1975>], u'NLE': [<Team:
Pittsburgh Pirates 1975>, <Team: Philadelphia Phillies 1975>, <Team:
New York Mets 1975>, <Team: St. Louis Cardinals 1975>, <Team: Chicago
Cubs 1975>, <Team: Montreal Expos 1975>], u'NLW': [<Team: Cincinnati
Reds 1975>, <Team: Los Angeles Dodgers 1975>, <Team: San Francisco
Giants 1975>, <Team: San Diego Padres 1975>, <Team: Atlanta Braves
1975>, <Team: Houston Astros 1975>]}
{'leagues': [u'AL', u'NL'], u'NLE': [<Team: Pittsburgh Pirates 1975>,
<Team: Philadelphia Phillies 1975>, <Team: New York Mets 1975>, <Team:
St. Louis Cardinals 1975>, <Team: Chicago Cubs 1975>, <Team: Montreal
Expos 1975>], u'NLW': [<Team: Cincinnati Reds 1975>, <Team: Los
Angeles Dodgers 1975>, <Team: San Francisco Giants 1975>, <Team: San
Diego Padres 1975>, <Team: Atlanta Braves 1975>, <Team: Houston Astros
1975>], 'year': u'1975', u'ALW': [<Team: Oakland Athletics 1975>,
<Team: Kansas City Royals 1975>, <Team: Texas Rangers 1975>, <Team:
Minnesota Twins 1975>, <Team: Chicago White Sox 1975>, <Team:
California Angels 1975>], u'ALE': [<Team: Boston Red Sox 1975>, <Team:
Baltimore Orioles 1975>, <Team: New York Yankees 1975>, <Team:
Cleveland Indians 1975>, <Team: Milwaukee Brewers 1975>, <Team:
Detroit Tigers 1975>], 'divisions': [u'E', u'W']}

--
Joel Goldstick
http://joelgoldstick.com

James Schneider

unread,
Sep 19, 2014, 5:44:04 AM9/19/14
to django...@googlegroups.com
Instead of "for team in {{l}}{{d}}" you should be able to do "for team in l.d" IIRC.

TBH I haven't tried it and I'm on my phone so I can't run a quick check. This SO post also has some hints on running nested loops: 
http://stackoverflow.com/questions/13870890/django-template-counter-in-nested-loops

-James

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPM-O%2ByfS1BfDCEKYbJOwctjtd8JkJ04OS71iv4dVtVRQf8KRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Sep 19, 2014, 6:14:02 AM9/19/14
to django...@googlegroups.com
Scratch that, I misread what you were trying to do, but became clear when I looked at your context a bit closer. You want to concatenate two strings and use that as the variable lookup.

You can probably do that using a combination of the {% with %} tag and the 'add' filter (which works on strings too), something like this:

{% with div=l|add:d %}


That's a L next to a pipe (|), in the event it isn't clear due to your variable name in the outer loop.

You may also need to build your context slightly different, and put the leagues you are trying to access within their own dict rather than at the top level of the context since you don't know what they will be named. Then you can put the following under the {% with %} tag:

{% for team in league_dict.div %}

Again, haven't tested so YMMV.

-James

Tom Evans

unread,
Sep 19, 2014, 10:33:17 AM9/19/14
to django...@googlegroups.com
On Fri, Sep 19, 2014 at 11:13 AM, James Schneider
<jrschn...@gmail.com> wrote:
> Scratch that, I misread what you were trying to do, but became clear when I
> looked at your context a bit closer. You want to concatenate two strings and
> use that as the variable lookup.
>
> You can probably do that using a combination of the {% with %} tag and the
> 'add' filter (which works on strings too), something like this:
>
> {% with div=l|add:d %}
>
> https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#with
>
> That's a L next to a pipe (|), in the event it isn't clear due to your
> variable name in the outer loop.
>
> You may also need to build your context slightly different, and put the
> leagues you are trying to access within their own dict rather than at the
> top level of the context since you don't know what they will be named. Then
> you can put the following under the {% with %} tag:
>
> {% for team in league_dict.div %}

This will not work, Django will not interpolate "div" to the value of
the "div" variable, it will look for an attribute (etc.) explicitly
called "div" on "league_dict".

It is easily doable by adding an additional template tag, usually
called something like dict_get. If you STFW, you will probably find an
implementation of it, probably from me...

https://groups.google.com/forum/#!topic/django-users/b0t9leTQyBA

It is not included in Django for ideological reasons - it is
considered better form to prepare your data correctly in the view than
manipulate it in the template.

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages