diff options
| author | e0ne <e0ne@e0ne.info> | 2013-09-13 18:11:48 +0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-04-08 13:55:17 -0400 |
| commit | 2aaa045c61a3d4b325f964aa01554107b47b9774 (patch) | |
| tree | 5b519e6adb79cdc10dfd0e4cca510e881aa1eaaa /django | |
| parent | b9bfcd82f0995b3bdd97a1c2cffa1cdd47ebc3c4 (diff) | |
Fixed #13408 -- Deprecated silent unpacking exception passing in for template tag.
Thanks peterbe for the suggestion.
Diffstat (limited to 'django')
| -rw-r--r-- | django/template/defaulttags.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 64fd86c042..c08f869e5f 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -17,6 +17,7 @@ from django.template.base import (Node, NodeList, Template, Context, Library, render_value_in_context) from django.template.smartif import IfParser, Literal from django.template.defaultfilters import date +from django.utils.deprecation import RemovedInDjango20Warning from django.utils.encoding import force_text, smart_text from django.utils.safestring import mark_safe from django.utils.html import format_html @@ -158,7 +159,8 @@ class ForNode(Node): nodelist = [] if self.is_reversed: values = reversed(values) - unpack = len(self.loopvars) > 1 + num_loopvars = len(self.loopvars) + unpack = num_loopvars > 1 # Create a forloop value in the context. We'll update counters on each # iteration just below. loop_dict = context['forloop'] = {'parentloop': parentloop} @@ -177,6 +179,21 @@ class ForNode(Node): if unpack: # If there are multiple loop variables, unpack the item into # them. + + # To complete this deprecation, remove from here to the + # try/except block as well as the try/except itself, + # leaving `unpacked_vars = ...` and the "else" statements. + if not isinstance(item, (list, tuple)): + len_item = 1 + else: + len_item = len(item) + # Check loop variable count before unpacking + if num_loopvars != len_item: + warnings.warn( + "Need {0} values to unpack in for loop; got {1}. " + "This will raise an exception in Django 2.0." + .format(num_loopvars, len_item), + RemovedInDjango20Warning) try: unpacked_vars = dict(zip(self.loopvars, item)) except TypeError: |
