Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django form errors. get the error without any html tags

I would like to get on my template the errors of the form in non-html version.

by default the error is wrap up by <ul class="errorlist"> which I want to avoid.

Anyway to do it without a massive code change ?

like image 759
Nuno_147 Avatar asked May 22 '13 14:05

Nuno_147


1 Answers

There are two new methods in Django 1.7 that would also be useful to solve this problem:

Form.errors.as_data()

>>> f.errors.as_data()
{'sender': [ValidationError(['Enter a valid email address.'])],
'subject': [ValidationError(['This field is required.'])]}

Form.errors.as_json()

>>> f.errors.as_json()
{"sender": [{"message": "Enter a valid email address.", "code": "invalid"}],
"subject": [{"message": "This field is required.", "code": "required"}]}
like image 157
Adam MacLeod Avatar answered Sep 19 '22 03:09

Adam MacLeod