From 4ead705cb3cf04bb7551ac037d1e11f682b62bcf Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Wed, 16 Aug 2017 17:57:59 +0530 Subject: [PATCH] Fixed #28502 -- Made stringformat template filter accept tuples --- django/template/defaultfilters.py | 2 ++ tests/template_tests/filter_tests/test_stringformat.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 6dafe70ba5..a1b08e0921 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -224,6 +224,8 @@ def stringformat(value, arg): for documentation of Python string formatting. """ try: + if isinstance(value, tuple): + return ('%' + str(arg)) % str(value) return ("%" + str(arg)) % value except (ValueError, TypeError): return "" diff --git a/tests/template_tests/filter_tests/test_stringformat.py b/tests/template_tests/filter_tests/test_stringformat.py index 9912f50842..8efa5e0599 100644 --- a/tests/template_tests/filter_tests/test_stringformat.py +++ b/tests/template_tests/filter_tests/test_stringformat.py @@ -30,6 +30,8 @@ class FunctionTests(SimpleTestCase): def test_format(self): self.assertEqual(stringformat(1, '03d'), '001') self.assertEqual(stringformat([1, None], 's'), '[1, None]') + self.assertEqual(stringformat((1, 2, 3), 's'), '(1, 2, 3)') + self.assertEqual(stringformat((1,), 's'), '(1,)') self.assertEqual(stringformat({1, 2}, 's'), '{1, 2}') self.assertEqual(stringformat({1: 2, 2: 3}, 's'), '{1: 2, 2: 3}')