Complete worked example
python
from bootstrap_lab import django_setup
django_setup(__name__)
from django import forms
class CandidateForm(forms.Form):
title = forms.CharField(max_length=20)
years = forms.IntegerField(min_value=1)
def validate_form(data):
form = CandidateForm(data)
if not form.is_valid():
return {"valid": False, "errors": sorted(form.errors)}
return {"valid": True, **form.cleaned_data}- Reasoning
- is_valid runs field conversion and validation, after which cleaned_data contains the accepted types. CharField strips surrounding whitespace under its default policy, while IntegerField converts suitable textual input and enforces the minimum.