From f7036b3e26745b944cd888fe7ed3f677b2caec8f Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Wed, 11 Oct 2017 11:09:25 +0200 Subject: [PATCH] Fixed #28662 -- Silenced join template filter error if arg isn't iterable. --- django/template/defaultfilters.py | 6 +++--- tests/template_tests/filter_tests/test_join.py | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index c857a0e0a3..d4bed6898d 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -518,11 +518,11 @@ def first(value): @register.filter(is_safe=True, needs_autoescape=True) def join(value, arg, autoescape=True): """Join a list with a string, like Python's ``str.join(list)``.""" - if autoescape: - value = [conditional_escape(v) for v in value] try: + if autoescape: + value = [conditional_escape(v) for v in value] data = conditional_escape(arg).join(value) - except AttributeError: # fail silently but nicely + except TypeError: # Fail silently if arg isn't iterable. return value return mark_safe(data) diff --git a/tests/template_tests/filter_tests/test_join.py b/tests/template_tests/filter_tests/test_join.py index 2d2a9cf78e..43ac0da7c2 100644 --- a/tests/template_tests/filter_tests/test_join.py +++ b/tests/template_tests/filter_tests/test_join.py @@ -65,3 +65,11 @@ class FunctionTests(SimpleTestCase): join(['', '', ''], '
', autoescape=False), '<br><br>', ) + + def test_noniterable_arg(self): + obj = object() + self.assertEqual(join(obj, '
'), obj) + + def test_noniterable_arg_autoescape_off(self): + obj = object() + self.assertEqual(join(obj, '
', autoescape=False), obj)