开发者

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():
            form.save()
            form = InternApplicationForm()
    else:
        form = InternApplicationForm()

    return render_to_response('application.html', {'form': form},
                              context_instance = RequestContext(r开发者_如何学Goequest))

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'


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 in self.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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜