Home » Django Form Validation

Django Form Validation

by Online Tutorials Library

Django Form Validation

Django provides built-in methods to validate form data automatically. Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data.

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

Let’s see an example that takes user input and validate input as well.

Django Validation Example

This example contains the following files and code.

// models.py

Now, create a form which contains the below code.

// forms.py

Instantiate the form

Instantiate the form, check whether request is post or not. It validate the data by using is_valid() method.

//views.py

Index template that shows form and errors.

// index.html

Start server and access the form.

django form validation

It validates each field and throws errors if any validation fails.

django form validation 1

Next TopicFile Upload

You may also like