Code

Ticket #13475: 13475.diff

File 13475.diff, 2.7 KB (added by SmileyChris, 4 years ago)
Line 
1diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
2index 318ae5f..d629a69 100644
3--- a/django/template/defaulttags.py
4+++ b/django/template/defaulttags.py
5@@ -157,15 +157,22 @@ class ForNode(Node):
6             loop_dict['first'] = (i == 0)
7             loop_dict['last'] = (i == len_values - 1)
8 
9+            pop_context = False
10             if unpack:
11                 # If there are multiple loop variables, unpack the item into
12                 # them.
13-                context.update(dict(zip(self.loopvars, item)))
14+                try:
15+                    unpacked_vars = dict(zip(self.loopvars, item))
16+                except TypeError:
17+                    pass
18+                else:
19+                    pop_context = True
20+                    context.update(unpacked_vars)
21             else:
22                 context[self.loopvars[0]] = item
23             for node in self.nodelist_loop:
24                 nodelist.append(node.render(context))
25-            if unpack:
26+            if pop_context:
27                 # The loop variables were pushed on to the context so pop them
28                 # off again. This is necessary because the tag lets the length
29                 # of loopvars differ to the length of each set of items and we
30diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
31index 5902e8d..8804839 100644
32--- a/tests/regressiontests/templates/tests.py
33+++ b/tests/regressiontests/templates/tests.py
34@@ -690,6 +690,7 @@ class Templates(unittest.TestCase):
35             'for-tag-unpack11': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, ("one:1,/two:2,/", "one:1,INVALID/two:2,INVALID/")),
36             'for-tag-unpack12': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2))}, ("one:1,carrot/two:2,/", "one:1,carrot/two:2,INVALID/")),
37             'for-tag-unpack13': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2, 'cheese'))}, ("one:1,carrot/two:2,cheese/", "one:1,carrot/two:2,cheese/")),
38+            'for-tag-unpack14': ("{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}", {"items": (1, 2)}, (":/:/", "INVALID:INVALID/INVALID:INVALID/")),
39             'for-tag-empty01': ("{% for val in values %}{{ val }}{% empty %}empty text{% endfor %}", {"values": [1, 2, 3]}, "123"),
40             'for-tag-empty02': ("{% for val in values %}{{ val }}{% empty %}values array empty{% endfor %}", {"values": []}, "values array empty"),
41             'for-tag-empty03': ("{% for val in values %}{{ val }}{% empty %}values array not found{% endfor %}", {}, "values array not found"),