Home > Software engineering >  Is there any way to get the value of multiplecheckboxes that the rendering in for loop with django-t
Is there any way to get the value of multiplecheckboxes that the rendering in for loop with django-t

Time:01-08

How to get value of mutiplecheckboxes in django.

the below code showing the groups at frontend

{% for group in groups %}
  <tr><td>{{group.name}}</td>
  <td><input type="checkbox" name="superuser" id="group-{{group.id}}" value="{{group.id}}"></td>                                             
 </tr>
{% endfor %}

I want to get the values of all checked boxes at backend when I submit the record.

frontend

CodePudding user response:

here is a way that i used and worked, try this may help you :)

in your html file:

{% for group in groups %}
  <tr><td>{{group.name}}</td>
  <td><input type="checkbox" name="superuser[]" id="group-{{group.id}}" value="{{group.id}}"></td>                                             
 </tr>
{% endfor %}

and in your view:

checked_group=request.POST.getlist('superuser[]')

so the checked_group will be a list contains all checked group ids.

  • Related