Django Form Validation Example

Published on Aug 01, 2022

Hi Dev,

This article will provide some of the most important example django python form validation example. I explained simply about how to add form validation in python django. Here you will learn django field validation example. Here you will learn django python form validation error example..

Let's see bellow example I explained simply about django field validation example.

Table of Contents

Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject exampleapp

Create an App

python3 startapp contactForm

Next, you need to add it in the file as follows:

Database Setup

Next step, we will modify the file and update the smtp email backends settings to configure:

    'default': {  
        'ENGINE': 'django.db.backends.mysql',  
        'NAME': 'example',  

Create a Model

In this step. we will require the database model for storing contacts.Open the contactForm/ file and add the following code:

from django.db import models

class Contact(models.Model):  
    name = models.CharField(max_length=100)
    email = models.EmailField()
    contact = models.CharField(max_length=100)

Create a FormClass

First of all in this step we will create a new file called and create a Django form you need to use Django Form Class

from django.forms import ModelForm
from django import forms
from core.models import Contact

class ContactForm(forms.ModelForm):
    name = forms.CharField(max_length=100,
                               widget=forms.TextInput(attrs={'placeholder': 'Name',
                                                             'class': 'form-control',
    email = forms.EmailField(required=True,
                             widget=forms.TextInput(attrs={'placeholder': 'Email',
                                                           'class': 'form-control',

    contact = forms.CharField(required=True,
                             widget=forms.TextInput(attrs={'placeholder': 'Contact',
                                                           'class': 'form-control',
    class Meta:  
        model = Contact  
        fields = "__all__" 

Creating the Views

In this step, we need to create the views for performing fetch records to the database. Open the contactForm/ file and add:

from django.shortcuts import render, redirect
from .forms import ContactForm

def contactForm(request):  
    if request.method == "POST":

        form = ContactForm(request.POST)

        if form.is_valid():  
                return redirect('contact')  

        form = ContactForm()
    return render(request,'contact.html',{'form':form})

Creating the Templates

Next, open the contactForm/templates/index.html file and the add:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Django Form Validation Example</title>
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h3>Django Form Validation Example - <span class="text-primary"></span></h3>
                    <div class="card-body">
                        <form method="POST" novalidate>
                            {% csrf_token %}
                            <div class="form-row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="small mb-1">Name:</label>
                                        {{ }}
                                        <small class="text-danger">{{|striptags }}</small>
                            <div class="form-row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="small mb-1">Email:</label>
                                        {{ }}
                                        <small class="text-danger">{{|striptags }}</small>
                            <div class="form-row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="small mb-1">Contact:</label>
                                        {{ }}
                                        <small class="text-danger">{{|striptags }}</small>
                            <div class="form-group mt-2 mb-0">
                                <button type="submit" class="btn btn-success">Submit</button>

Creating Urls

In this section, we’ll create the urls to access our views.Go to the contactForm/ file and update it as follows:

from django.urls import path
from . import views

urlpatterns = [
    path('contact-us', views.contactForm, name='contact'),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('contactForm.urls')),

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the url address bar with a web browser.


I Hope It will help you.

Comments (0)