Django Form.is_valid Keeps Throwing Keyerror
I have this code in my view: def add_intern(request): if request.method == 'POST': form = InternApplicationForm(request.POST) if form.is_valid(): fo
Solution 1:
Okay, so I just nailed it down.
I followed this advice to set my custom error message for validation. So I had this code:
def__init__(self, *args, **kwargs):
super(InternApplicationForm, self).__init__(*args, **kwargs)
for field inself.fields.values():
field.error_messages = {'required':'*'}
that set the same required field validation message for all fields.
When the error was different (invalid
for non-integer), Django looked in the dictionary I supplied—and guess what, KeyError
. Because there is no message for invalid
there (and that's my fault).
So the fix is
field.error_messages = {'required': '*', 'invalid': "That's not a number, sir."}
(and possibly other error message keys)
Post a Comment for "Django Form.is_valid Keeps Throwing Keyerror"