It clearly cannot display fields in the context, because these are โnon-field errors,โ as the name of the attribute implies. The only way to fix this is to add the error in the right place when checking. For example, doing the following results with borderless errors:
class MyModelForm(forms.ModelForm): class Meta: model = MyModel def clean(self): somefield = self.cleaned_data.get('somefield') if not somefield: raise forms.ValidationError('Some field is blank')
However, you can do the following so that this error appears in the right field:
class MyModelForm(forms.ModelForm): class Meta: model = MyModel def clean(self): somefield = self.cleaned_data.get('somefield') if not somefield: if not self._errors.has_key('somefield'): from django.forms.util import ErrorList self._errors['somefield'] = ErrorList() self._errors['somefield'].append('Some field is blank')
UPDATE:
From the Django docs :
Each named form field can be output to the template using {{form.name_of_field}}, which will call the HTML code required to display the form widget. Using {{form.name_of_field.errors}} displays a list of form errors displayed as an unordered list. It might look like this:
<ul class="errorlist"> <li>Sender is required.</li> </ul>
There is a CSS error class in the list that allows you to appear. If you want to further customize the display of errors, you can do this by going through them (my attention):
{% if form.subject.errors %} <ol> {% for error in form.subject.errors %} <li><strong>{{ error|escape }}</strong></li> {% endfor %} </ol> {% endif %}
Chris Pratt Sep 14 '11 at 16:45 2011-09-14 16:45
source share