Solution 1 :

This work.
Just make a change handler in jquery and prove if it is checked or not.
Check it in the Developer Tools.

$('input[type="checkbox"]').change(function() {
  if (this.checked) {
    $(this).val(1);
  } else {
    $(this).val(0);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <input name="need_a" type="checkbox" class="custom-control-input" value="1" checked>
  <label for="scales">Scales</label>
</div>

<div>
  <input name="need_a" type="checkbox" class="custom-control-input" value="1" checked>
  <label for="horns">Horns</label>
</div>

<div>
  <input name="need_a" type="checkbox" class="custom-control-input" value="1" checked>
  <label for="Guitar">Guitar</label>
</div>

Solution 2 :

Your approach is incorrect, what you need is to create a hidden input with the default value for the unchecked fields an keep the grouped by name, see my example below:

<input name="need_a" type="hidden" value="0">
<input name="need_a" type="checkbox" class="custom-control-input" value="1" {% if some_condition %}checked{% endif %}>

<input name="need_b" type="hidden" value="0">
<input name="need_b" type="checkbox" class="custom-control-input " value="1" {% if some_condition %}checked{% endif %} />

<input name="need_c" type="hidden" value="0">
<input name="need_c" type="checkbox" class="custom-control-input" value="1" {% if some_condition %}checked{% endif %} />

And in django you need to create an array to respect your format such as:

formValues = [request.POST.getlist('need_a'), request.POST.getlist('need_b') , request.POST.getlist('need_c')]

Then you’ll get [1,0,1] / [0,0,1] / etc values.

This is what the browser is sending to django (need_a checked, need_b not checked, need_c checked):

need_a  […]
0   "0"
1   "1"
need_b  "0"
need_c  […]
0   "0"
1   "1

And this is what my backend(PHP) caught on the request:

Array ( [need_a] => 1 [need_b] => 0 [need_c] => 1 ) 

Problem :

Here I have multiple checkbox with same name. Checkbox can be added to n no. of times.

Here if already checked I want to send 1 and if not 0.

Right now in my django backend I am getting the list with request.POST.getlist('need_a') like this.

If all checked [1, 1, 1] which is fine .

if only one checked then I am getting [1] instead of [1, 0, 0]

 <input name="need_a" type="checkbox" class="custom-control-input" 
 value="1" {% if some_condition %}checked{% endif %}/>

 <input name="need_a" type="checkbox" class="custom-control-input "
 value="1" {% if some_condition %}checked{% endif %}/>

 <input name="need_a" type="checkbox" class="custom-control-input" 
 value="1" {% if some_condition %}checked{% endif %}/>

Comments

Comment posted by Rüzgar

Hi, where is the jQuery in this example? You can easily get the desired array with jQuery and than send it via ajax post request to your python code.

Comment posted by D_P

@Rüzgar I am not being able to write jquery for this

Comment posted by moddayjob

The problem is, when it is not checked, the value is not sent. Even if you set the value to 0, django won’t receive it.

Comment posted by D_P

thanks for the answer but it didn’t worked.

Comment posted by Bernhard Beatus

Ok. Sorry I’m not familiar with django.

Comment posted by D_P

Isn’t it possible all the checkbox with same names ?

Comment posted by darklightcode

No, that’s not how you use checkboxes/radios, they’re meant to send only 1 value. The fact that you send your format and django catches

Comment posted by darklightcode

I’ve updated my answer, as you can see

By