Opened 10 years ago

Closed 10 years ago

#24284 closed Cleanup/optimization (invalid)

List-index lookup in templates does not function as advertised

Reported by: Matt Kahl Owned by: nobody
Component: Documentation Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In the Django template language docs (https://docs.djangoproject.com/en/1.6/ref/templates/api/) the list-index lookup syntax is advertised as list[index] but a TemplateSyntaxError is thrown when using that syntax. The example provided shows list.index which, while arguably less intuitive, does work.

>>> from django.template import Template, Context
>>> Template("The first element is {{ items[0] }}").render(Context({"items":["spam"]}))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/mattkahl/.venvs/sp_web/lib/python2.7/site-packages/django/template/base.py", line 125, in __init__
    self.nodelist = compile_string(template_string, origin)
  File "/Users/mattkahl/.venvs/sp_web/lib/python2.7/site-packages/django/template/base.py", line 153, in compile_string
    return parser.parse()
  File "/Users/mattkahl/.venvs/sp_web/lib/python2.7/site-packages/django/template/base.py", line 254, in parse
    filter_expression = self.compile_filter(token.contents)
  File "/Users/mattkahl/.venvs/sp_web/lib/python2.7/site-packages/django/template/base.py", line 360, in compile_filter
    return FilterExpression(token, self)
  File "/Users/mattkahl/.venvs/sp_web/lib/python2.7/site-packages/django/template/base.py", line 577, in __init__
    "from '%s'" % (token[upto:], token))
TemplateSyntaxError: Could not parse the remainder: '[0]' from 'items[0]'
>>> Template("The first element is {{ items.0 }}").render(Context({"items":["spam"]}))
u'The first element is spam'

Tested in 1.6. The list[index] syntax is referenced all the way back to the 1.3 docs.

This is either a feature request, a bug report, or a documentation error.

Change History (4)

comment:1 by Matt Kahl, 10 years ago

Summary: List-index lookup does not function as advertisedList-index lookup in templates does not function as advertised

comment:2 by Aymeric Augustin, 10 years ago

Are you referring to this?

List-index lookup. Example: foo[bar]

It's an illustration in Python code of how foo.bar may be interpreted. It isn't template syntax.

comment:3 by Matt Kahl, 10 years ago

Yep. Now I see. Potentially a little ambiguous in the display of that information, but ultimately a non-issue.

Thanks for the heads up.

comment:4 by Matt Kahl, 10 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top