From dbde7fc8b0932a169b8319dc597433aef1cb9520 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 26 Jun 2008 04:30:06 +0000 Subject: [PATCH] Fixed #6322 -- Fixed bug in 'ifchanged' template tag where it wasn't resetting itself properly in some cases. Thanks, nedbatchelder git-svn-id: http://code.djangoproject.com/svn/django/trunk@7752 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaulttags.py | 4 +++- tests/regressiontests/templates/tests.py | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index cf3b35b4cc..2d887d022e 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -162,10 +162,12 @@ class IfChangedNode(Node): self.nodelist = nodelist self._last_seen = None self._varlist = map(Variable, varlist) + self._id = str(id(self)) def render(self, context): - if 'forloop' in context and context['forloop']['first']: + if 'forloop' in context and self._id not in context['forloop']: self._last_seen = None + context['forloop'][self._id] = 1 try: if self._varlist: # Consider multiple parameters. This automatically behaves diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index d3331d032d..809b4c6934 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -135,8 +135,7 @@ class Templates(unittest.TestCase): # Quickly check that we aren't accidentally using a name in both # template and filter tests. - overlapping_names = [name for name in filter_tests if name in - template_tests] + overlapping_names = [name for name in filter_tests if name in template_tests] assert not overlapping_names, 'Duplicate test name(s): %s' % ', '.join(overlapping_names) template_tests.update(filter_tests) @@ -159,7 +158,7 @@ class Templates(unittest.TestCase): # Turn TEMPLATE_DEBUG off, because tests assume that. old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, False - # Set TEMPLATE_STRING_IF_INVALID to a known string + # Set TEMPLATE_STRING_IF_INVALID to a known string. old_invalid = settings.TEMPLATE_STRING_IF_INVALID expected_invalid_str = 'INVALID' @@ -549,6 +548,7 @@ class Templates(unittest.TestCase): 'ifchanged05': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', {'num': (1, 1, 1), 'numx': (1, 2, 3)}, '1123123123'), 'ifchanged06': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', {'num': (1, 1, 1), 'numx': (2, 2, 2)}, '1222'), 'ifchanged07': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% for y in numy %}{% ifchanged %}{{ y }}{% endifchanged %}{% endfor %}{% endfor %}{% endfor %}', {'num': (1, 1, 1), 'numx': (2, 2, 2), 'numy': (3, 3, 3)}, '1233323332333'), + 'ifchanged08': ('{% for data in datalist %}{% for c,d in data %}{% if c %}{% ifchanged %}{{ d }}{% endifchanged %}{% endif %}{% endfor %}{% endfor %}', {'datalist': [[(1, 'a'), (1, 'a'), (0, 'b'), (1, 'c')], [(0, 'a'), (1, 'c'), (1, 'd'), (1, 'd'), (0, 'e')]]}, 'accd'), # Test one parameter given to ifchanged. 'ifchanged-param01': ('{% for n in num %}{% ifchanged n %}..{% endifchanged %}{{ n }}{% endfor %}', { 'num': (1,2,3) }, '..1..2..3'),