mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	[1.10.x] Fixed #27058 -- Reallowed the {% for %} tag to unpack any iterable.
Thanks Sergei Maertens for the report and patch.
Backport of 937d752d3d from master
			
			
This commit is contained in:
		| @@ -189,10 +189,10 @@ class ForNode(Node): | |||||||
|                 if unpack: |                 if unpack: | ||||||
|                     # If there are multiple loop variables, unpack the item into |                     # If there are multiple loop variables, unpack the item into | ||||||
|                     # them. |                     # them. | ||||||
|                     if not isinstance(item, (list, tuple)): |                     try: | ||||||
|                         len_item = 1 |  | ||||||
|                     else: |  | ||||||
|                         len_item = len(item) |                         len_item = len(item) | ||||||
|  |                     except TypeError:  # not an iterable | ||||||
|  |                         len_item = 1 | ||||||
|                     # Check loop variable count before unpacking |                     # Check loop variable count before unpacking | ||||||
|                     if num_loopvars != len_item: |                     if num_loopvars != len_item: | ||||||
|                         raise ValueError( |                         raise ValueError( | ||||||
|   | |||||||
| @@ -51,3 +51,5 @@ Bugfixes | |||||||
|  |  | ||||||
| * Fixed annotations with database functions when combined with lookups on | * Fixed annotations with database functions when combined with lookups on | ||||||
|   PostGIS (:ticket:`27014`). |   PostGIS (:ticket:`27014`). | ||||||
|  |  | ||||||
|  | * Reallowed the ``{% for %}`` tag to unpack any iterable (:ticket:`27058`). | ||||||
|   | |||||||
| @@ -126,6 +126,11 @@ class ForTagTests(SimpleTestCase): | |||||||
|         output = self.engine.render_to_string('for-tag-filter-ws', {'s': 'abc'}) |         output = self.engine.render_to_string('for-tag-filter-ws', {'s': 'abc'}) | ||||||
|         self.assertEqual(output, '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 %}'}) |     @setup({'for-tag-unpack10': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'}) | ||||||
|     def test_for_tag_unpack10(self): |     def test_for_tag_unpack10(self): | ||||||
|         with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 3.'): |         with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 3.'): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user