Django Form Password Validation Example

Published On: 03/10/2022 | Category: Django

Hi Dev,

Today, I will let you know example of django form password validation example. Here you will learn django form auth password validation example . you can see django form validation password example tutorial. this example will help you django form validation example.

The validate password function is a part of Django by default. It is employed to verify the user's entered password. The password validator's purpose is to make sure that the password is not too basic.

  • Similarity:Its check password must not be similar to username, first_name, last_name, and email.
  • Minimum length:It checks the minimum length of the password. By default, the minimum length is 8.
  • Common Password:Django has a list of 20.000 records of common passwords.
  • Numeric:Password must not be fully numeric.

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

Step 1: 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

Step 2: Create a App

Now we'll create a single app called polls to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 startapp core

Step 3: Update

Next, In this step we require to do two things in our file, One is our installed app name and another is define these password validators.Add the below lines to your file:


        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        'OPTIONS': {'min_length':6}
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',

Step 4: Create a Form

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 import forms
from matplotlib import widgets
from django.contrib.auth import password_validation

class EmpRegistration(forms.Form):
    username = forms.CharField(label='Username',
                                    'class': 'form-control'
    password = forms.CharField(label='Password',
                                        'class': 'form-control'
    confirm_Password = forms.CharField(label='Confirm Password',
                                                'class': 'form-control'

    def clean(self):
        cleaned_data = super().clean()
        password1= self.cleaned_data['password']
        cpassword= self.cleaned_data['confirm_Password']

        if password1 != cpassword:
            raise forms.ValidationError('Confirm Password is not same as Password')
        return self.cleaned_data

Step 5: Creating the Views

In this step, we need to create the views for performing the post request data is to cleaned to cleaned_data method.Open the core/ file and add:

from django.shortcuts import render
from .forms import EmpRegistration

def detail_form(request):
    if request.method == "POST":  
        form = EmpRegistration(request.POST)
        if form.is_valid():
            print('Username:', form.cleaned_data['username'])            
            print('Password:', form.cleaned_data['password']) 
            print('Confirm Password:', form.cleaned_data['confirm_Password'])
        form =EmpRegistration()
    return render(request,'home.html',{'form':form}) 

Step 6: Creating the Templates

Next, open the core/templates/home.html file and the add:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <style type="text/css">
            background-color: #f7fcff;
    <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">
                        <h4>Django Form Password Validation Example - <span class="text-primary"></span></h4>
                    <div class="card-body">
                        <form method = "post" enctype="multipart/form-data">
                            {% csrf_token %}
                            {{ form.as_p }}
                            <button type="submit" class="btn btn-success">Submit</button>

Step 7: Creating Urls

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

from django.urls import path, include
from . import views

urlpatterns = [
    path('password', views.detail_form,name='detail_form'),

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('core.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....