form.is_valid() do in Django forms?form.is_valid()?Calling form.is_valid() runs all validation rules on the form fields. It returns True if all data is valid, otherwise False. This prevents bad data from being processed.
form.cleaned_data after validation?form.is_valid() and it returns True, what does form.cleaned_data contain?from django import forms class SampleForm(forms.Form): name = forms.CharField(max_length=10) age = forms.IntegerField() form = SampleForm({'name': 'Alice', 'age': '30'}) if form.is_valid(): data = form.cleaned_data else: data = None
form.cleaned_data holds the validated and converted data from the form fields as a dictionary. For example, strings converted to integers where needed.
clean method in a Django form?clean_fieldname methods and accesses cleaned_data.Custom validation for a single field uses a method named clean_fieldname. It accesses self.cleaned_data to get the field value and returns the cleaned value or raises ValidationError.
is_valid()?form.is_valid() always return False?
class MyForm(forms.Form):
email = forms.EmailField()
form = MyForm({'email': 'not-an-email'})
valid = form.is_valid()The string 'not-an-email' is not a valid email format, so the EmailField validation fails and is_valid() returns False.
form.cleaned_data before calling form.is_valid()?form = MyForm({'name': 'Bob'})
data = form.cleaned_data
What will happen when you try to access form.cleaned_data before calling form.is_valid()?cleaned_data is only populated after is_valid() runs validation. Accessing it before causes an AttributeError because it does not exist yet.