Ticket #13475: 13475.diff

File 13475.diff, 2.7 KB (added by SmileyChris, 5 years ago)
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 318ae5f..d629a69 100644
    a b class ForNode(Node): 
    157157            loop_dict['first'] = (i == 0)
    158158            loop_dict['last'] = (i == len_values - 1)
    159159
     160            pop_context = False
    160161            if unpack:
    161162                # If there are multiple loop variables, unpack the item into
    162163                # 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)
    164171            else:
    165172                context[self.loopvars[0]] = item
    166173            for node in self.nodelist_loop:
    167174                nodelist.append(node.render(context))
    168             if unpack:
     175            if pop_context:
    169176                # The loop variables were pushed on to the context so pop them
    170177                # off again. This is necessary because the tag lets the length
    171178                # of loopvars differ to the length of each set of items and we
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 5902e8d..8804839 100644
    a b class Templates(unittest.TestCase): 
    690690            '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/")),
    691691            '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/")),
    692692            '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/")),
     693            'for-tag-unpack14': ("{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}", {"items": (1, 2)}, (":/:/", "INVALID:INVALID/INVALID:INVALID/")),
    693694            'for-tag-empty01': ("{% for val in values %}{{ val }}{% empty %}empty text{% endfor %}", {"values": [1, 2, 3]}, "123"),
    694695            'for-tag-empty02': ("{% for val in values %}{{ val }}{% empty %}values array empty{% endfor %}", {"values": []}, "values array empty"),
    695696            'for-tag-empty03': ("{% for val in values %}{{ val }}{% empty %}values array not found{% endfor %}", {}, "values array not found"),
Back to Top