On Wed, Mar 21, 2012 at 2:58 PM, Larry Martell <larry.mart...@gmail.com> wrote:
> OK, but is there some reason {{ headers.0.forloop.counter } does not
> work when forloop.counter has a value of 2, yet {{ headers.0.2 }}
> does work?

Django never does variable interpolation when resolving dot separated variables.

What django does do when it resolves dot separated variables is well documented:

https://docs.djangoproject.com/en/1.3/topics/templates/#variables

So outputting 'headers.0.forloop.counter' first finds the 'headers'
variable, and gets the 0th entry. In fact, first of all it tries

  headers.get('0')

IE, a dictionary lookup. This fails, so it tries

  getattr(headers, '0')

IE, an attribute lookup. This fails, so it tries

  headers.0() *

IE, a method call. This fails, so it tries

  headers[0]

IE, a list index lookup.

Having got 'headers.0' resolved, it would then step on to the next
part of the lookup - 'forloop'.

I think if you trace through yourself what it will do to lookup
forloop as a dictionary entry of headers[0], an attribute of
headers[0], a method call of headers[0] or a list index lookup of
headers[0], you will see that obviously all of these would fail. If
you replace forloop.counter with the raw value '2' and repeat the
process, you'll see why that works as well.

Cheers

Tom

* It doesn't actually do this, I haven't checked the code, but it
probably does something like this:

  val = headers.get('0')
  if not val:
    val = getattr(headers, '0')
    if callable(val):
      return val()
  if not val:
    val = headers[0]
  return val

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to