#35738: Deprecate double-dot variable lookups
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Type:
| Cleanup/optimization
Status: new | Component: Template
| system
Version: dev | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Currently, a double-dot variable lookup, like `{{ book..title }}`, is
allowed in templates. It maps to a lookup of the empty string before the
next lookup of the named attribute.
Supporting double-dot lookups means that when written accidentally, the
user gets a silent failure, as for a missing variable, rather than a
syntax error. The empty string lookup is unlikely to be used
intentionally.
This behaviour is also inconsistent with Python and Jinja, which both
raise a syntax error.
Let’s deprecated `..` in Django templates, eventually turning it into a
syntax error. We should be able to start by adding the warning in
`Variable.__init__`:
{{{#!diff
--- django/template/base.py
+++ django/template/base.py
@@ -842,6 +842,8 @@ def __init__(self, var):
"not begin with underscores: '%s'" % var
)
self.lookups =
tuple(var.split(VARIABLE_ATTRIBUTE_SEPARATOR))
+ if "" in self.lookups:
+ warnings.warn(...)
def resolve(self, context):
"""Resolve this variable against a given context."""
}}}
The warning message should include the template file and line.
This ticket follows [
https://forum.djangoproject.com/t/deprecate-in-
template-variable-lookups/34180 this forum discussion], where Carlton +1'd
me on the concept.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35738>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.