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 manage.py startapp core
Step 3 : Update setting.py

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

....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]
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/views.py file and add.

core/views.py
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:

core/templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Get Checkbox Values in Django?</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
    <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">Tuts-Station.com</span></h4>
                    </div>
                    <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>
                            <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>
                            </div>
                            <button type="submit" class="btn btn-success mt-3">Submit</button>
                        </form>
                        <div class="row mt-3">
                            <div class="col-md-12">
                                {% if languages %}
                                    {{ languages }}
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Step 6 : Creating URLs

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

core/urls.py
from django.urls import path
from . import views

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

Next, we will require the modify the urls.py your root preoject folder lets update the file.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    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 manage.py runserver

Next, go to the address with a web browser.

http://localhost:8000/home/

I Hope It will help you....