[Answered ]-Django how to store data from checkbox into a database

1👍

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}
👤Niko

Leave a comment