Tsallaka zuwa abun ciki
Justice Tech School

Form Validation

Add clean_<field> methods for one field or clean() for several fields. Raise ValidationError to show a message.

Example
class SignupForm(forms.Form):
    username = forms.CharField()

    def clean_username(self):
        name = self.cleaned_data["username"]
        if name.lower() == "admin":
            raise forms.ValidationError("That name is reserved.")
        return name

Exercise

Reject bad input.

raise forms.Error("bad")

Quiz Test

Where is single field cleaning done?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.