Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a form field 'error' on view

Tags:

django

I want to know in my view, if a certain field raised an error, that way I can send some values to the template (index of current tab (using a javascript tab widget)). Is it possible?

Thanks in advance.

like image 691
luismmontielg Avatar asked Jun 28 '11 18:06

luismmontielg


1 Answers

You can definitely access your form errors in your view. Each bound Form instance has an errors attribute that gives you a dictionary mapping field name to error-message lists:

>>> f = ContactForm({'subject': 'Hello', 'message':''})
>>> f.errors
{'message':[u'This field is required.']}

You can access individual fields as follows:

>>> if f['subject'].errors:
        values = [Add values to send to template]
like image 164
rolling stone Avatar answered Nov 10 '22 03:11

rolling stone