From 549b65824184711823e6a8c38ccf7365f10023d8 Mon Sep 17 00:00:00 2001 From: Ben Davis Date: Sat, 3 May 2014 12:02:07 -0500 Subject: [PATCH] [1.7.x] Fixed #22570 -- Made Form.__getitem__ KeyError more descriptive. Backport of df60db0e78 from master --- django/forms/forms.py | 3 ++- tests/forms_tests/tests/test_forms.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/django/forms/forms.py b/django/forms/forms.py index 19164aacae1..5362fd5aaf0 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -142,7 +142,8 @@ class BaseForm(object): try: field = self.fields[name] except KeyError: - raise KeyError('Key %r not found in Form' % name) + raise KeyError( + "Key %r not found in '%s'" % (name, self.__class__.__name__)) return BoundField(self, field, name) @property diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index c05c79a68ea..ea2858200cc 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -57,11 +57,11 @@ class FormsTestCase(TestCase): self.assertHTMLEqual(str(p['first_name']), '') self.assertHTMLEqual(str(p['last_name']), '') self.assertHTMLEqual(str(p['birthday']), '') - try: + + nonexistenterror = "Key u?'nonexistentfield' not found in 'Person'" + with self.assertRaisesRegexp(KeyError, nonexistenterror): p['nonexistentfield'] self.fail('Attempts to access non-existent fields should fail.') - except KeyError: - pass form_output = []