Django - To get form.errors as a dictionary instead of HTML code

django, django-forms, python

Solution

There's not built-in method for return errors as `dict`, but you can use json.

form = MyForm(data)
print(form.errors.as_json())

doc for `errors.as_json`

Problem

Is there a way to get the form errors generated during the django form validation in a dictionary (key as the 'field_name' and value as 'the list of errors relevant to it'), instead of the default HTML code it generates (ul & li combination). I'm not using the generated HTML code, and I'm just bothered about the field name and the errors.

Original source