Form.is_valid()
The primary task of a Form
object is to validate data. With a bound Form
instance, call the is_valid()
method to run validation and return a boolean designating whether the data was valid:
>>> data = {'subject': 'hello', ... 'message': 'Hi there', ... 'sender': 'foo@example.com', ... 'cc_myself': True} >>> f = ContactForm(data) >>> f.is_valid() True
Let’s try with some invalid data. In this case, subject
is blank (an error, because all fields are required by default) and sender
is not a valid email address:
>>> data = {'subject': '', ... 'message': 'Hi there', ... 'sender': 'invalid email address', ... 'cc_myself': True} >>> f = ContactForm(data) >>> f.is_valid() False
Please login to continue.