Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Add non_field_error from view?

I've got a Django form with a bunch of fields that I'm rendering in the template. I've also got some straight HTML input elements which I want to validate in the view by accessing the request.POST vars. If those don't validate, I want to inject an error into the Django form so I can display it on the page. Is there a way to do that?

like image 823
mpen Avatar asked Nov 19 '10 20:11

mpen


1 Answers

You can also use quite elegant add_error() method. Works for Django >= 1.7.

If you set field as None form will treat the error as "non_field" one. So:

form.add_error(None, "I'm non-field error")

works like a charm.

like image 127
n1_ Avatar answered Oct 05 '22 22:10

n1_