Permalink
Browse files

Fixed #22570 -- Made Form.__getitem__ KeyError more descriptive.

  • Loading branch information...
bendavis78 authored and timgraham committed May 3, 2014
1 parent 6923fdb commit df60db0e7872796b4a48e62e9733f85c8ddf8306
Showing with 5 additions and 4 deletions.
  1. +2 −1 django/forms/forms.py
  2. +3 −3 tests/forms_tests/tests/test_forms.py
View
@@ -142,7 +142,8 @@ def __getitem__(self, name):
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
@@ -58,11 +58,11 @@ def test_form(self):
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 = []

0 comments on commit df60db0

Please sign in to comment.