summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Phalip <jphalip@gmail.com>2013-02-14 23:29:15 -0800
committerJulien Phalip <jphalip@gmail.com>2013-02-15 00:18:49 -0800
commit42e87c17f27f68ea43c78938d4972211b141ede3 (patch)
tree6dede06fb6c3338d7495959e2d672b5ff95aadba
parentbc6746ac303ab625f2bc6fc878bd63661c784a59 (diff)
[1.5.x] Fixed #19829 -- Fixed index lookups for NumPy arrays in templates.
Backport of 7d5e35cdb46124e2471
-rw-r--r--django/template/base.py2
-rw-r--r--tests/regressiontests/templates/tests.py17
2 files changed, 18 insertions, 1 deletions
diff --git a/django/template/base.py b/django/template/base.py
index 0a2b2c9437..8ee7723b68 100644
--- a/django/template/base.py
+++ b/django/template/base.py
@@ -756,7 +756,7 @@ class Variable(object):
for bit in self.lookups:
try: # dictionary lookup
current = current[bit]
- except (TypeError, AttributeError, KeyError):
+ except (TypeError, AttributeError, KeyError, ValueError):
try: # attribute lookup
current = getattr(current, bit)
except (TypeError, AttributeError):
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 8d2a45b8fc..255671435a 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -54,6 +54,12 @@ except ImportError as e:
else:
raise
+# NumPy installed?
+try:
+ import numpy
+except ImportError:
+ numpy = False
+
from . import filters
#################################
@@ -1649,6 +1655,17 @@ class Templates(TestCase):
'verbatim-tag05': ('{% verbatim %}{% endverbatim %}{% verbatim %}{% endverbatim %}', {}, ''),
'verbatim-tag06': ("{% verbatim special %}Don't {% endverbatim %} just yet{% endverbatim special %}", {}, "Don't {% endverbatim %} just yet"),
}
+
+ if numpy:
+ tests.update({
+ # Numpy's array-index syntax allows a template to access a certain item of a subscriptable object.
+ 'numpy-array-index01': ("{{ var.1 }}", {"var": numpy.array(["first item", "second item"])}, "second item"),
+
+ # Fail silently when the array index is out of range.
+ 'numpy-array-index02': ("{{ var.5 }}", {"var": numpy.array(["first item", "second item"])}, ("", "INVALID")),
+ })
+
+
return tests
class TemplateTagLoading(unittest.TestCase):