diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index b3060202be..a211323320 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -26,7 +26,7 @@ class MergeDict(object): return dict_[key] except KeyError: pass - raise KeyError + raise KeyError(key) def __copy__(self): return self.__class__(*self.dicts) diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py index 91111cc2ed..5829e7c2d7 100644 --- a/tests/utils_tests/test_datastructures.py +++ b/tests/utils_tests/test_datastructures.py @@ -209,6 +209,14 @@ class MergeDictTests(SimpleTestCase): self.assertFalse(empty) self.assertTrue(not_empty) + def test_key_error(self): + """ + Test that the message of KeyError contains the missing key name. + """ + d1 = MergeDict({'key1': 42}) + with six.assertRaisesRegex(self, KeyError, 'key2'): + d1['key2'] + class MultiValueDictTests(SimpleTestCase):