Django Admin Form Validation Error Example

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

Hi Dev,

This article is focused on django admin form validation error example. you can see django admin form validation. We will use django admin form validation error example. I’m going to show you about django admin form validation example tutorial.

We'll observe a form validation problem on the admin side in this section.

Let's see bellow example I explained simply about django admin form validation error 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 Add the below lines to your file:


Step 4: Create a Model

Now go for the models we will We'll call our single model Employee And finally set __str__ to display the name of the Employee in admin interface.

from django.db import models

# Create your models here.
class Employee(models.Model):
    emp_id = models.IntegerField(unique=True)
    name = models.CharField(max_length=200)
    assignment_assign = models.CharField(max_length=500)
    start_date = models.DateField()
    end_date = models.DateField()

    def __str__(self):
        return self.emp_id

Step 5: 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 .models import Employee

class EmployeeForm(forms.ModelForm):

    emp_id = forms.CharField(label='Employee Id',
                                    'class': 'form-control'
    name = forms.CharField(label='Name',
                                    'class': 'form-control'
    assignment_assign = forms.CharField(label='Assignment Assign',
                                    'class': 'form-control'

    start_date = forms.CharField(label='Start Date',
                                    'class': 'form-control'
    end_date = forms.CharField(label='End Date',
                                    'class': 'form-control'
    class Meta:
        model = Employee
        fields = ['emp_id', 'name', 'assignment_assign', 'start_date', 'end_date']

Step 6: 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 EmployeeForm

def admin_vali(request):
    if request.method == "POST":  
        form = EmployeeForm(request.POST)
        if form.is_valid():
            print('Employee Id:', form.cleaned_data['emp_id'])
            print('Employee Name:', form.cleaned_data['name'])            
            print('Assignment Assign:', form.cleaned_data['assignment_assign']) 
            print('Start Date:', form.cleaned_data['start_date'])
            print('End Date:', form.cleaned_data['end_date'])
        form =EmployeeForm()
    return render(request,'form.html',{'form':form})

Step 7: Creating the Templates

Next, open the core/templates/form.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">
        <div class="row d-flex justify-content-center">
            <div class="col-md-10">
                <div class="card">
                    <div class="card-header">
                        <h3>Django Admin Form Validation Error Example - <span class="text-primary"></span></h3>
                    <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 8: 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('admin-form', views.admin_vali,name='admin_vali'),

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....