Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13475 - for tag raises an exception when trying to unpack a no…

…n-iterable item

Thanks to SmileyChris for the report and patch.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@13690 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0b37d56481d453e79b322fef28313c83bb806561 1 parent 4f4e20b
Luke Plant authored September 07, 2010
11  django/template/defaulttags.py
@@ -157,15 +157,22 @@ def render(self, context):
157 157
             loop_dict['first'] = (i == 0)
158 158
             loop_dict['last'] = (i == len_values - 1)
159 159
 
  160
+            pop_context = False
160 161
             if unpack:
161 162
                 # If there are multiple loop variables, unpack the item into
162 163
                 # them.
163  
-                context.update(dict(zip(self.loopvars, item)))
  164
+                try:
  165
+                    unpacked_vars = dict(zip(self.loopvars, item))
  166
+                except TypeError:
  167
+                    pass
  168
+                else:
  169
+                    pop_context = True
  170
+                    context.update(unpacked_vars)
164 171
             else:
165 172
                 context[self.loopvars[0]] = item
166 173
             for node in self.nodelist_loop:
167 174
                 nodelist.append(node.render(context))
168  
-            if unpack:
  175
+            if pop_context:
169 176
                 # The loop variables were pushed on to the context so pop them
170 177
                 # off again. This is necessary because the tag lets the length
171 178
                 # of loopvars differ to the length of each set of items and we
1  tests/regressiontests/templates/tests.py
@@ -701,6 +701,7 @@ def get_template_tests(self):
701 701
             '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/")),
702 702
             '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/")),
703 703
             '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/")),
  704
+            'for-tag-unpack14': ("{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}", {"items": (1, 2)}, (":/:/", "INVALID:INVALID/INVALID:INVALID/")),
704 705
             'for-tag-empty01': ("{% for val in values %}{{ val }}{% empty %}empty text{% endfor %}", {"values": [1, 2, 3]}, "123"),
705 706
             'for-tag-empty02': ("{% for val in values %}{{ val }}{% empty %}values array empty{% endfor %}", {"values": []}, "values array empty"),
706 707
             'for-tag-empty03': ("{% for val in values %}{{ val }}{% empty %}values array not found{% endfor %}", {}, "values array not found"),

0 notes on commit 0b37d56

Please sign in to comment.
Something went wrong with that request. Please try again.