Form validation (is_valid, cleaned_data)
📖 Scenario: You are building a simple Django web app where users can submit their name and age through a form.We want to check if the form data is valid and then access the cleaned data safely.
🎯 Goal: Create a Django form with fields for name and age. Then validate the form using is_valid() and access the cleaned data using cleaned_data.
📋 What You'll Learn
Create a Django form class called
PersonForm with name (CharField) and age (IntegerField).Create a dictionary called
form_data with 'name': 'Alice' and 'age': 30.Instantiate
PersonForm with form_data.Check if the form is valid using
form.is_valid() and then access form.cleaned_data.💡 Why This Matters
🌍 Real World
Forms are used in web apps to collect user input like registration, login, or surveys. Validating forms ensures data is correct before saving or processing.
💼 Career
Understanding Django form validation is essential for backend web developers working with user input and data integrity.
Progress0 / 4 steps