Django forms are incredibly flexible, but they're also a little cumbersome when it comes to errors.
The following example shows you that you can display an error for:
- a specific field
- a specific field from another field
- the form as a whole
model = Event
title = forms.CharField(required = True)
lawyer = forms.CharField(required = True)
when = forms.CharField(max_length = 10, min_length = 10, widget = DateInput(format="%d-%m-%Y"))
value = self.cleaned_data.get(field_name)
value = datetime.datetime.strptime(value, '%d-%m-%Y')
value = None
# Displays an error under the title field, even though it's validating as the "when" field.
self._errors['title'] = ['Invalid date given.']
# Display error for the lawyer field
data = super(EventForm, self).clean()
# In the clean() even, errors raised will display as an error for the form (non field error)
raise forms.ValidationError('Just for the hell of it.')
Raising an error is the preferred way of doing things, but if you need to do multi-line errors then it's possible to use the self._errors method.