Friday, September 19, 2014

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

On Fri, Sep 19, 2014 at 11:13 AM, James Schneider
<jrschneider83@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

--
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/CAFHbX1%2BdRiLDkDZrh3F%3DrFmdTR3WdRTW2GKnVgW755k_Cjvwcg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment