Max Rukhlov
Max Rukhlov

Reputation: 331

Passing checkboxes values to view in django

I have a table with some database data

<table id="id_list_table" class="table table-condensed">
<caption>Inputs list</caption>
    <thead>
      <tr>
        <th>#</th>
        <th id="input">Input</th>
      </tr>
    </thead>
    <tbody id="fbody">
    {%for input in InputsAll%}
        <tr>
            <td>{{ input.input }}</td>
            <td>
                <a class="btn btn-primary btn-xs" href="{% url 'edit' %}?input_num={{input.id}}" id="edit">edit</a>
                <a class="btn btn-danger btn-xs" href="{% url 'delete' %}?input_num={{input.id}}" id="remove">remove</a>
                <a class="btn btn-success btn-xs" href="{% url 'resolve' %}?input_num={{input.id}}"id="resolve">resolve</a>
                <input type="checkbox" name="inputs" id="option{{input.id}}" value={{input.id}} />
            </td>
        </tr>
    {%endfor%}
    </tbody>
</table>

and i want to add a checkbox to every field and pass checked data's id to view and make group action instead of removing items one by one. i added this checkbox

<input type="checkbox" name="inputs" id="option{{input.id}}" value={{input.id}} />

and how should i pass all checked values to view? Does it work this way?

Upvotes: 5

Views: 8873

Answers (1)

Amar
Amar

Reputation: 666

views.py

if request.method == 'POST':
        #gives list of id of inputs 
        list_of_input_ids=request.POST.getlist('inputs')

Hope this solves pretty much of your problem.Check out this link Checkboxes for a list of items like in Django admin interface

Upvotes: 7

Related Questions