Friday, September 19, 2014

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

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

On Friday, September 19, 2014, James mSchneider <jrschneider83@gmail.com> wrote:
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


On Thursday, September 18, 2014, Joel Goldstick <joel.goldstick@gmail.com> wrote:
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

--
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 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.

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

No comments:

Post a Comment