#15708: Regression in Forms: KeyError if trying to access nonvalid input from required field --------------------------------------+---------------------- Reporter: jonescb | Owner: nobody Status: new | Milestone: Component: Forms | Version: 1.3 Resolution: | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | --------------------------------------+---------------------- Description changed by kmtracey:
Old description: > I discovered that I'm getting a KeyError when I try to access a blank > required field from the clean() method of the Form. > > Something like this will trigger the error: > > class TestForm(forms.Form) > name = forms.CharField() > > def clean(self): > super(forms.Form, self).clean() > name = self.cleaned_data['name'] # this is the offending line > return self.cleaned_data > > If you display that form in a template and the user enters nothing into > the field, it causes a KeyError which leads to a 500 error. This same > code used to work in Django 1.2, but it doesn't work in 1.3 anymore. > > If you set the field to required=False, it does work. > The obvious workaround is to wrap the offending line in a try/except. > > The previous functionality was that Django would display an error to the > user telling them that the field is required if you configured your > templates to display the errors. It still displays that error if you > don't try to access keys from self.cleaned_data. > > This also happens with EmailField if you enter an invalid email address. > I didn't test any other fields though. New description: I discovered that I'm getting a KeyError when I try to access a blank required field from the clean() method of the Form. Something like this will trigger the error: {{{ class TestForm(forms.Form) name = forms.CharField() def clean(self): super(forms.Form, self).clean() name = self.cleaned_data['name'] # this is the offending line return self.cleaned_data }}} If you display that form in a template and the user enters nothing into the field, it causes a KeyError which leads to a 500 error. This same code used to work in Django 1.2, but it doesn't work in 1.3 anymore. If you set the field to required=False, it does work. The obvious workaround is to wrap the offending line in a try/except. The previous functionality was that Django would display an error to the user telling them that the field is required if you configured your templates to display the errors. It still displays that error if you don't try to access keys from self.cleaned_data. This also happens with EmailField if you enter an invalid email address. I didn't test any other fields though. -- -- Ticket URL: <http://code.djangoproject.com/ticket/15708#comment:2> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.