Thursday, September 8, 2011

Re: How do I render a template variable inside a for loop, when the var is a dict and the key is forloop.counter

Thanks.
But note that I'm not looking for the dict value in MyList, rather: I
loop on MyList and (trying to) retrieve the dict value from (another
var) BOOK_PHONE with key=loop.counter.
I think that makes it a bit more challenging, but still it's a fairly
simple case. It's this limitation in the template syntax ... that
doesn't allow you to do MyDict[key], instead you need to write
MyDict.key (dot). But my key is "loop.counter", and
MyDict.loop.counter (2 dots) is too much for the template to be
interpreted correctly.


On Sep 8, 12:51 pm, Andre Terra <andrete...@gmail.com> wrote:
> There is an inflexibility indeed, because the template is meant for
> designers, not programmers.
>
> Try {% for key, value in MyList.items %} instead.
>
> A hackier solution would be to write your custom template tag like {%
> get_from_dict foo bar %} to try to get foo[bar]. But in this case you can
> just use items(), as it's often the case when iterating over dictionaries.
>
> Cheers,
> AT
>
>
>
>
>
>
>
> On Thu, Sep 8, 2011 at 4:24 PM, Rami <rrr...@gmail.com> wrote:
> > Hi,
> > I have a dict var defined in server side, like PHONE_BOOK={1:'634534',
> > 2:'264886', 3:'455346' etc..}
> > Then in my django template I have this loop where I need to print the
> > PHONE_BOOK[loop counter]:
> > {% for Item in MyList %}
>
> >          Item.first_name |  PHONE_BOOK.forloop.counter
>
> > {% endfor %}
> > ---------------------------------
> > For some reason PHONE_BOOK.forloop.counter doesn't work and neither
> > the more direct ways like PHONE_BOOK[forloop.counter]; it seems to me
> > that there's a bit of an inflexibility in the django template renderin
> > mechanism.
>
> > Any idea how to solve that?
>
> > Thanks.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django-users@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate