How to Add Input Mask using JQuery in Django?

Published On: 08/08/2022 | Category: Django

This tutorial will give you example of how to add input mask using jquery in python django. we will help you to give example of how to add input mask using jquery in django. I’m going to show you about django python input mask using jquery. let’s discuss about input mask using jquery.

Let's see bellow example I explained simply about django python input mask using jquery.

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
python3 startapp contactForm

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

Step 3 : 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):  
    contact = models.CharField(max_length=100)
Step 4 : 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):
    contact = forms.CharField(required=True,
                             widget=forms.TextInput(attrs={'placeholder': 'Contact',
                                                           'class': 'form-control inputmask',
    class Meta:  
        model = Contact  
        fields = "__all__" 
Step 5 : Creating the Views

In this step, we need to create the views for performing fetch record 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})
Step 6 : Creating the Templates

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

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>How To Add Input Mask Using Jquery In Django Python ?</title>
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
     <script src=""></script>
    <script src=""></script>
    <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>How To Add Input Mask In Django Forms ? - <span class="text-primary"></span></h4>
                    <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">Contact:</label>
                                        {{ }}
                            <div class="form-group mt-2 mb-0">
                                <button type="submit" class="btn btn-success">Submit</button>
<script type="text/javascript">
Step 7 : 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('', 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....