Code

Opened 9 years ago

Closed 8 years ago

#449 closed enhancement (wontfix)

[patch] variable selection for templates should allow negative list indexes

Reported by: hugo <gb@…> Owned by: adrian
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: UI/UX:

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.

Attachments (0)

Change History (3)

comment:1 Changed 9 years ago by Boffbowsh

  • Summary changed from variable selection for templates should allow negative list indexes to [patch] variable selection for templates should allow negative list indexes
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'

comment:2 Changed 9 years ago by anonymous

  • Cc paul.bowsher@… added

comment:3 Changed 8 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

I'm marking this as a wontfix because it's too much of an edge case.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.