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)