I have this code in my view:
def add_intern(request):
if request.method == 'POST':
form = InternApplicationForm(request.POST)
if form.is_valid():
form.save()
form = InternApplicationForm()
else:
form = InternApplicationForm()
return render_to_response('application.html', {'form': form},
context_instance = RequestContext(request))
The form is a ModelForm
, and the underlying model contains an IntegerField
.
When I post a form with empty value, the validation message is displayed just fine.
When I post the form with a non-integer value, I get this:
KeyError at /
'invalid'
It kind of surprises me that the code seems to crash on is_valid()
call, which I assumed is safe (i.e. should return False
if there is a problem and not just crash). How do I fix this?
Stacktrace
Django Version: 1.3
Python Version: 2.6.5
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/dan/www/ints/backend/views.py" in add_intern
14. if form.is_valid():
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/forms/forms.py" in is_valid
121. return self.is_bound and not bool(self.errors)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/forms/forms.py" in _get_errors
112. self.full_clean()
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/forms/forms.py" in full_clean
267. self._clean_fields()
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/forms/forms.py" in _clean_fields
284. value = field.clean(value)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/forms/fields.py" in clean
169. value = self.to_python(value)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/forms/fields.py" in to_python
248. raise ValidationError(self.error_messages['invalid'])
Exception Type: KeyError at /
Exception Value: 'invalid'