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

DjangoDjango Forms

Django Problem Overview


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__']

Django Solutions


Solution 1 - Django

{{ form.non_field_errors }}

Solution 2 - Django

{{ form.non_field_errors }} for errors related to form not field

{{ form.password.errors }} for errors related to text-field like passoword in this case

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThierry LamView Question on Stackoverflow
Solution 1 - DjangoDmitry ShevchenkoView Answer on Stackoverflow
Solution 2 - DjangoAbhayaView Answer on Stackoverflow