Why value is not Inserting in the database using django

Question:

I want to insert value in the database from checklist. it print me the value but does’t show anything in the database. it’s empty. sample output what I’m getting if ISMS(which is on the third place) is checked [00100] the zero changes accordingly but this data is not inserting in the data base

below is my code::

Views.py

from collections import Counter
from .models import details
from django.shortcuts import render
from .forms import CheckBoxForm

# 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':
        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')

def form_checkbox(request):
    if request.method == 'POST':
        form = CheckBoxForm(request.POST or None)
        if form.is_valid():
            print(form.cleaned_data)
            prod = form.save(commit=False) # error here in save 
            prod.save()    
    else:
        form = CheckBoxForm()    
    context = {'form': form}
    return render(request, 'checkbox.html', context)

urls.py:

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),
    path('about/', views.about),
    path('checkbox/', views.checkbox)
]

forms.py

from django import forms
from .models import details

class CheckBoxForm(forms.ModelForm):
    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)

    class Meta:
        model = details
        fields = ('exec_summary', 'scope', 'isms', 'methodology', 'recommendation')

        widgets = {
            'exec_summary': forms.BooleanField(),
            'scope': forms.BooleanField(),
            'isms': forms.BooleanField(),
            'methodology': forms.BooleanField(),
            'recommendation': forms.BooleanField(),
        }

models.py:

from django.db import models
from django.db.models import Model
class details(models.Model):
    exec_summary = models.BooleanField('exec_summary', default=False)
    scope = models.BooleanField('scope', default=False)
    isms = models.BooleanField('isms', default=False)
    methodology = models.BooleanField('methodology', default=False)
    recommendation = models.BooleanField('recommendation', default=False)

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="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>
</body>
</html>

I did some changes in the above code like. in forms.py file I change the forms.Form to forms.ModelForm. so now data is inserting in the database but everything is 0. mean that whenn I check the checkbox the data is not inserting accordingly mean that value need to be 1 but it still insert 0 and output is now like this:: {'exec_summary': False, 'scope': False, 'isms': False, 'methodology': False, 'recommendation': False}

Asked By: khan

||

Answers:

There is an error in your urls. You do not set the good view for posting the form.

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),
    path('about/', views.about),
    path('checkbox/', views.form_checkbox)  # <-- Here
]

You have to changed too your html. your input name need to be same as the form field :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Checkbox</title>
</head>
<body>
<form action="" method="post">
 {% csrf_token %}
 {{ form.as_p }}
 <button type="submit">submit</button>
</form>
</body>
</html>
Answered By: Lucas Grugru