from django import forms class EmailForm(forms.Form): email = forms.EmailField() form = EmailForm(data={'email': ''}) valid = form.is_valid() errors = form.errors.get('email')
Django forms automatically check required fields. If the email is empty, is_valid() returns False and errors contains a message explaining the problem.
form.cleaned_data contain after calling form.is_valid() with valid input?from django import forms class NameForm(forms.Form): name = forms.CharField() form = NameForm(data={'name': 'Alice'}) form.is_valid() result = form.cleaned_data
When is_valid() returns True, cleaned_data contains the validated and cleaned form input as a dictionary.
Option D correctly defines clean_age which accesses cleaned_data, raises an error if invalid, and returns the cleaned value.
Other options either use wrong method names or access data incorrectly.
form.is_valid():from django import forms class ProductForm(forms.Form): name = forms.CharField() price = forms.DecimalField() def clean(self): if self.cleaned_data['price'] <= 0: raise forms.ValidationError('Price must be positive') return self.cleaned_data form = ProductForm(data={'name': 'Book'}) form.is_valid()
The form data lacks 'price', so cleaned_data does not have 'price' key when clean runs, causing a KeyError.
To fix, use self.cleaned_data.get('price') or check if 'price' is present before accessing.
Django forms simplify creating HTML forms, validate user input safely, and help protect against security risks such as Cross-Site Request Forgery (CSRF).
Other options are incorrect because forms do not replace JavaScript, do not create database tables, and do not bypass validation.