diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index a08ce2710dc..02cac06bcfd 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -644,7 +644,7 @@ def slice_filter(value, arg): bits.append(int(x)) return value[slice(*bits)] - except (ValueError, TypeError): + except (ValueError, TypeError, KeyError): return value # Fail silently. diff --git a/tests/template_tests/filter_tests/test_slice.py b/tests/template_tests/filter_tests/test_slice.py index 5a5dd6b1550..23257b1282b 100644 --- a/tests/template_tests/filter_tests/test_slice.py +++ b/tests/template_tests/filter_tests/test_slice.py @@ -53,3 +53,6 @@ class FunctionTests(SimpleTestCase): def test_fail_silently(self): obj = object() self.assertEqual(slice_filter(obj, "0::2"), obj) + + def test_empty_dict(self): + self.assertEqual(slice_filter({}, "1"), {})