Django how to store data from checkbox into a database

I want to add data from checkbox to database. I watched some videos but didn't worked out.

checkbox.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Checkbox</title>
</head>
<body>
<form action="" method="post">
{%csrf_token%}
    <div class="form-check">

        <h5>Checkbox_report</h5>
        <input type="checkbox" value="Executive_summary" id="Executive_summary" name="checkbox_data">
        <label for="Executive_summary"> Executive summary &nbsp</label>
        <input type="checkbox" value="Scope" id="Scope" name="checkbox_data">
        <label for="Scope"> Scope &nbsp</label>
        <input type="checkbox" value="ISMS" id="ISMS" name="checkbox_data">
        <label for="ISMS"> ISMS &nbsp</label>
        <input type="checkbox" value="Methodology" id="Methodology" name="checkbox_data">
        <label for="Methodology"> Methodology &nbsp</label>
        <input type="checkbox" value="Recommendation" id="Recommendation" name="checkbox_data">
        <label for="Recommendation"> Recommendation &nbsp</label>

    </div>
 <button type="submit">submit</button>
</form>
</body>
</html>

views.py

from collections import Counter
from django.shortcuts import render
# Create your views here.
def home(request):
    return render(request, 'home.html', {"text": "hello home"})
def about(request):
    return render(request, 'about.html', {"text": "hello about"})

def checkbox(request):
    if request.method == 'POST':
        checkbox_data = request.POST.getlist('checkbox_data')
        for i in checkbox_data:
            print(i)
    return render(request, 'checkbox.html')

I want to add 1 when user check any of the checkbox and the one which are not checked add 0

second:: what is forms.py?what is the main difference between forms.py and manage.py

First, to directly solve your question, in raw format. Following this answer:

checkbox.html

<form action="" method="post">
{%csrf_token%}
    <div class="form-check">
        <h5>Checkbox_report</h5>
        <input type="hidden" name="Executive_summary" value="0" />
        <input type="checkbox" name="Executive_summary" value="1" id="Executive_summary" />
        <label for="Executive_summary"> Executive summary &nbsp</label>

        <input type="hidden" name="Scope" value="0" />
        <input type="checkbox" name="Scope" value="1" id="Scope" />
        <label for="Scope"> Scope &nbsp</label>

        <input type="hidden" name="ISMS" value="0" />
        <input type="checkbox" name="ISMS" value="1" id="ISMS" />
        <label for="ISMS"> ISMS &nbsp</label>

        <input type="hidden" name="Methodology" value="0" />
        <input type="checkbox" name="Methodology" value="1" id="ISMS" />
        <label for="Methodology"> Methodology &nbsp</label>

        <input type="hidden" name="Recommendation" value="0" />
        <input type="checkbox" name="Recommendation" value="1" id="Recommendation" />
        <label for="Recommendation"> Recommendation &nbsp</label>
    </div>
 <button type="submit">submit</button>
</form>

views.py:

def checkbox(request):
    if request.method == 'POST':
        exec_summary = request.POST.get('Executive_summary')
        scope = request.POST.get('Scope')
        isms = request.POST.get('ISMS')
        methodology = request.POST.get('Methodology')
        recommendation = request.POST.get('Recommendation')
        print(f'{exec_summary}{scope}{isms}{methodology}{recommendation}')

    return render(request, 'checkbox.html')

# Print Output Sample (0s and 1s):
10100

Now, to answer your second question and to give you further example:

Models are maps to database tables (generally one model is related to a single table), with models Django gives you an automatically-generated database-access API (In other words an Object-relational mapping or ORM).

On the other hand, Forms are used to handle input from your visitors / users. Django forms offers you a variety of features, that brings you benefits such as easier way of doing validations and have a much cleaner code. (Of course there are trade-offs)

So, we can argue that the main difference between them is that one is directly related to the database and the other is not.

As example, lets transform this code into a Django form:

forms.py: (boolean fields are automatically rendered as checkboxes)

from django import forms

class CheckBoxForm(forms.Form):
    exec_summary = forms.BooleanField(required=False)
    scope = forms.BooleanField(required=False)
    isms = forms.BooleanField(required=False)
    methodology = forms.BooleanField(required=False)
    recommendation = forms.BooleanField(required=False)

form_checkbox.html:

<body>
    <form action="" method="post">
        {%csrf_token%}
        {{form.as_p}}
        <button type="submit">submit</button>
    </form>
</body>

views.py:

from .forms import CheckBoxForm

def form_checkbox(request):
    if request.method == 'POST':
        form = CheckBoxForm(request.POST or None)

        if form.is_valid():
            print(form.cleaned_data)
    else:
        form = CheckBoxForm()

    context = {'form': form}
    return render(request, 'form_checkbox.html', context)

# Print Output Sample
{'exec_summary': True, 'scope': False, 'isms': True, 'methodology': False, 'recommendation': False}
Back to Top