How to Get Checkbox Values in Django?

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

Hi Dev,

This simple article demonstrates of how to get checkbox values in django. if you have question about how to get multiple checkbox value in django then I will give simple example with solution. I would like to show you django html checkbox value. let’s discuss about how to get multiple checkbox'' value in django.

In this example, we will use getlist to get the all values from checkbox in django application

You can use these examples with django3 (django 3) version.

let's see below simple example with output:

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 example
Step 2 : Create a App
python3 startapp core
Step 3 : Update

Here, do not forget to register the new app in the file. Under installed apps, just add ‘core’ to the list:

Step 4 : Creating the Views

In this step, we need to create the views for performing the get checkbox value to the form submit.Open the core/ file and add.

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def home(request):
    languages = []
    if request.method == 'POST':
        languages = request.POST.getlist('languages')
    return render(request, 'index.html',{'languages':languages})
Step 5 : Creating the Templates

So, finally we are here to this step we need to creating django templates following below file:

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

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>How to Get Checkbox Values in Django?</title>
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <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 Get Checkbox Values in Django? - <span class="text-primary"></span></h4>
                    <div class="card-body">

                        <form method="post">
                            {% csrf_token %}
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" value="Python" id="python" name="languages">
                                <label class="form-check-label" for="python">Python</label>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" value="PHP" id="php" name="languages">
                                <label class="form-check-label" for="php">PHP</label>
                            <button type="submit" class="btn btn-success mt-3">Submit</button>
                        <div class="row mt-3">
                            <div class="col-md-12">
                                {% if languages %}
                                    {{ languages }}
                                {% endif %}
Step 6 : 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
from . import views

urlpatterns = [
    path('home/', views.home, name='home')

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 address with a web browser.


I Hope It will help you....