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'
--
Ticket URL: <https://code.djangoproject.com/ticket/20423>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Definitely not full fix, it does cause regressions elsewhere - namely the
view loading a changelist in the Admin.
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:1>
Comment (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?
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:2>
* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:3>
* owner: nobody => iapain
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:4>
Comment (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?
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:5>
Comment (by anonymous):
Makes more sense as a doc ticket, agreed! Thanks for looking!
Antonio
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:6>
* owner: Deepak => (none)
* needs_better_patch: 1 => 0
* has_patch: 1 => 0
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:7>
* owner: (none) => Tim McCurrach
* status: new => assigned
Comment:
I have created a PR to update the docs. Given it will now be documented,
adding in any checks or warnings seems like overkill to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:8>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/13990 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:9>
* type: Bug => Cleanup/optimization
* component: Template system => Documentation
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:10>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:11>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"7587139d35b630a19fdc55a5f3789475e5ed26c8" 7587139d]:
{{{
#!CommitTicketReference repository=""
revision="7587139d35b630a19fdc55a5f3789475e5ed26c8"
Fixed #20423 -- Doc'd that DTL variable names may not be a number.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:12>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"7e6e822162c0b567e81d7b4c3a61e6f1041be49f" 7e6e8221]:
{{{
#!CommitTicketReference repository=""
revision="7e6e822162c0b567e81d7b4c3a61e6f1041be49f"
[3.2.x] Fixed #20423 -- Doc'd that DTL variable names may not be a number.
Backport of 7587139d35b630a19fdc55a5f3789475e5ed26c8 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:13>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/20423#comment:14>