Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display the Django '__all__' form errors in the template?

I have the following form code:

# forms.py class SomeForm(forms.Form):     hello = forms.CharField(max_length=40)     world = forms.CharField(max_length=40)      def clean(self):         raise forms.ValidationError('Something went wrong')  # views.py def some_view(request):     if request.method == 'POST':         form = SomeForm(request.POST)         if form.is_valid():             pass     else:         form = SomeForm()      data = {         'form': form     }     return render_to_response(         'someform.html',         data,         context_instance=RequestContext(request)     )  # someform.html {{ form.hello }} {{ form.hello.errors }}  {{ form.world }} {{ form.world.errors }} 

How can I display the errors from the key __all__ at the template level without having to extract it in the view separately? I want to avoid the following:

    if form.errors.has_key('__all__'):         print form.errors['__all__'] 
like image 722
Thierry Lam Avatar asked Mar 26 '10 18:03

Thierry Lam


People also ask

How do I show errors in Django?

The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override. For example, here is the default error message: >>> from django import forms >>> generic = forms.

How do I display validation error in Django?

To display the form errors, you use form. is_valid() to make sure that it passes validation. Django says the following for custom validations: Note that any errors raised by your Form.

Which method has a form instance which runs validation routines for all its fields?

The run_validators() method on a Field runs all of the field's validators and aggregates all the errors into a single ValidationError . You shouldn't need to override this method.


1 Answers

{{ form.non_field_errors }} 
like image 137
Dmitry Shevchenko Avatar answered Sep 22 '22 06:09

Dmitry Shevchenko