diff options
| author | Tim Graham <timograham@gmail.com> | 2016-08-15 15:39:22 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-15 15:39:22 -0400 |
| commit | 937d752d3deabebe60dfbe9ff9823772730f336a (patch) | |
| tree | e9eab6551a8d0a3fd3eceb8df2243e399620bddf | |
| parent | 21aec54296708ae29a06d9a5b97ba665bed44394 (diff) | |
Fixed #27058 -- Reallowed the {% for %} tag to unpack any iterable.
Thanks Sergei Maertens for the report and patch.
| -rw-r--r-- | django/template/defaulttags.py | 6 | ||||
| -rw-r--r-- | docs/releases/1.10.1.txt | 2 | ||||
| -rw-r--r-- | tests/template_tests/syntax_tests/test_for.py | 5 |
3 files changed, 10 insertions, 3 deletions
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 624f07da7b..ce2265fde1 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -189,10 +189,10 @@ class ForNode(Node): if unpack: # If there are multiple loop variables, unpack the item into # them. - if not isinstance(item, (list, tuple)): - len_item = 1 - else: + try: len_item = len(item) + except TypeError: # not an iterable + len_item = 1 # Check loop variable count before unpacking if num_loopvars != len_item: raise ValueError( diff --git a/docs/releases/1.10.1.txt b/docs/releases/1.10.1.txt index fdc00861c6..eebe4e44d1 100644 --- a/docs/releases/1.10.1.txt +++ b/docs/releases/1.10.1.txt @@ -51,3 +51,5 @@ Bugfixes * Fixed annotations with database functions when combined with lookups on PostGIS (:ticket:`27014`). + +* Reallowed the ``{% for %}`` tag to unpack any iterable (:ticket:`27058`). diff --git a/tests/template_tests/syntax_tests/test_for.py b/tests/template_tests/syntax_tests/test_for.py index 03c2e6ee9b..4af7881e57 100644 --- a/tests/template_tests/syntax_tests/test_for.py +++ b/tests/template_tests/syntax_tests/test_for.py @@ -126,6 +126,11 @@ class ForTagTests(SimpleTestCase): output = self.engine.render_to_string('for-tag-filter-ws', {'s': 'abc'}) self.assertEqual(output, 'abc') + @setup({'for-tag-unpack-strs': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'}) + def test_for_tag_unpack_strs(self): + output = self.engine.render_to_string('for-tag-unpack-strs', {'items': ('ab', 'ac')}) + self.assertEqual(output, 'a:b/a:c/') + @setup({'for-tag-unpack10': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'}) def test_for_tag_unpack10(self): with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 3.'): |
