Django Form Checkbox Validation Example

Published On: 30/09/2022 | Category: Django

Hi Dev,

This simple article demonstrates of django form checkbox validation example. I would like to show you django form checkbox. if you have question about django form validation before submit then I will give simple example with solution. I explained simply step by step django form validation example. You just need to some step to done how to use checkbox form validation in django.

A checkbox is a button with two states: checked and unchecked. The default widget for checkbox input is CheckboxInput. A boolean field in a Django form is a checkbox field that only accepts True or False values. The empty value is False by default.

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


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

class EmpDetailsForm(forms.Form):
    agree = forms.BooleanField(
                            required = True,
                            disabled = False,
                                attrs={'class': 'checkbox-inline'}),
                                help_text = "I accept the terms in the License Agreement",
                                error_messages ={'required':'Please check the box'}

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 EmpDetailsForm

def emp_form(request):
    error = {} 
    form = EmpDetailsForm(request.POST)
    error['form'] = form

    if request.POST:
        if form.is_valid():
                return redirect('/')  
    return render(request,'form.html',error)  

Step 6: 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 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-9">
                <div class="card">
                    <div class="card-header">
                        <h3>Django Form Checkbox Validation 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 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('form', views.emp_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....