Code

Opened 11 months ago

Last modified 11 months ago

#20423 assigned Bug

Template renderer does not parse numeric variables

Reported by: antonio@… Owned by: iapain
Component: Template system Version: master
Severity: Normal Keywords: number variable
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Currently a Variable is defined, according to the django template manual, as :

Variable names consist of any combination of alphanumeric characters and the underscore ("_").

If a variable is composed of numbers only, it will not get its context data correctly, coming across as an int. This patch assigns the appropriate context data to the Variable class when it encounters an integer.

Here's the current behavior:

from django.template.base import Template
from django.template.base import Context
t = Template("{{ 123 }}")
t.render(Context())

u'123'

t = Template("{{ foo }}")
t.render(Context({"foo":"bar"}))

u'bar'

Attachments (1)

number_templating.diff (515 bytes) - added by antonio@… 11 months ago.
Patch to assign context to numeric Variables in Django templating

Download all attachments as: .zip

Change History (7)

Changed 11 months ago by antonio@…

Patch to assign context to numeric Variables in Django templating

comment:1 Changed 11 months ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Definitely not full fix, it does cause regressions elsewhere - namely the view loading a changelist in the Admin.

comment:2 Changed 11 months ago by charettes

The issue is valid but IMHO the code should be fixed to disallow such uses of number variables and the documentation fixed accordingly (e.g Variable name can't start with an integer).

I know this might be backward incompatible but I can't see any valid use case for this behavior, looks more like a side effect/oversight of the access by index feature. Am I missing something here?

comment:3 Changed 11 months ago by charettes

  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 11 months ago by iapain

  • Owner changed from nobody to iapain
  • Status changed from new to assigned

comment:5 Changed 11 months ago by iapain

I totally agree with charettes that there may not be an use case for this. Hence, I'd propose to add warning first and then raise an exceptions if integer or float variable names are used. Does it makes sense?

comment:6 Changed 11 months ago by anonymous

Makes more sense as a doc ticket, agreed! Thanks for looking!

Antonio

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as assigned
The owner will be changed from iapain to anonymous. Next status will be 'assigned'
The ticket will be disowned. Next status will be 'new'
as The resolution will be set. Next status will be 'closed'
Author


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

 
Note: See TracTickets for help on using tickets.