Opened 20 years ago
Closed 19 years ago
#449 closed enhancement (wontfix)
[patch] variable selection for templates should allow negative list indexes
| Reported by: | Owned by: | Adrian Holovaty | |
|---|---|---|---|
| Component: | Template system | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | paul.bowsher@… | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Currently you can access elements of a list by adding the numeric index - but you can only use positive indexes. This should be extended to negative indexes as well, so that you could last elements in lists.
Change History (3)
comment:1 by , 20 years ago
| Summary: | variable selection for templates should allow negative list indexes → [patch] variable selection for templates should allow negative list indexes |
|---|
comment:2 by , 20 years ago
| Cc: | added |
|---|
comment:3 by , 19 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
I'm marking this as a wontfix because it's too much of an edge case.
Note:
See TracTickets
for help on using tickets.
Index: /usr/local/django_src/django/core/template.py =================================================================== --- /usr/local/django_src/django/core/template.py (revision 593) +++ /usr/local/django_src/django/core/template.py (working copy) @@ -71,7 +71,7 @@ VARIABLE_TAG_START = '{{' VARIABLE_TAG_END = '}}' -ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.' +ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-' # match a variable or block tag and capture the entire tag, including start/end delimiters tag_re = re.compile('(%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),Test:
>>> from django.core import template >>> t = '''{{ l.-1 }}''' >>> l = [5, 2, 9, 4] >>> >>> tem = template.Templat l = [5, 2, 9, 4] KeyboardInterrupt >>> tem = template.Template(t) >>> c = template.Context({'l': l}) >>> tem.render(c) '4'