diff options
| author | Ben Davis <bendavis78@gmail.com> | 2014-05-03 12:02:07 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-05-05 20:01:15 -0400 |
| commit | df60db0e7872796b4a48e62e9733f85c8ddf8306 (patch) | |
| tree | 17c0163d9cee43823899736d168e642b21b482ee | |
| parent | 6923fdbbf1824d6e28cc1d283b48146c7f58c6ea (diff) | |
Fixed #22570 -- Made Form.__getitem__ KeyError more descriptive.
| -rw-r--r-- | django/forms/forms.py | 3 | ||||
| -rw-r--r-- | 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 af7318255f..917987c0be 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 051968d14f..b8c5f28d2f 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -58,11 +58,11 @@ class FormsTestCase(TestCase): self.assertHTMLEqual(str(p['first_name']), '<input type="text" name="first_name" value="John" id="id_first_name" />') self.assertHTMLEqual(str(p['last_name']), '<input type="text" name="last_name" value="Lennon" id="id_last_name" />') self.assertHTMLEqual(str(p['birthday']), '<input type="text" name="birthday" value="1940-10-9" id="id_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 = [] |
