Python, Django: how to get checked table rows into lists?

Tags: , , , ,

Good evening,

I’m hoping someone knows an answer to my problem of getting all checked rows inside a table into a multidimensional list (after submit)?

I know I can do something like this to get – for example – all the checked ids into a list:


<form action="" method="post">
    {% csrf_token %}

    <div class="table-wrapper">
        <table class="table table-hover">
                    <th scope="col"></th>
                    <th scope="col">id</th>
                    <th scope="col">country</th>
                    <th scope="col">continent</th>
                    <th scope="col">...</th>
                {% for item in data %}
                            <input type="checkbox" name="list_gap" value="{{ }}">
                        <td>{{ }}</td>
                        <td>{{ item.continent}}</td>

if request.method == 'POST':
    checked_data = request.POST.getlist('list_gap')

But as mentioned, this will only get me a list of the checked ids! Is there a way to get all the data of the row into a multidimensional list, for example like..?

list_checked = [
    [1, 'country', 'continent', '...'],
    [2, 'country', 'continent', '...'],
    [3, 'country', 'continent', '...'],

Thanks for all your help and a good night!


I’d recommend creating a queryset to pull the instances from the DB.

checked_data = request.POST.getlist('list_gap')
checked_instances = Model.objects.filter(id__in=checked_data)

Source: stackoverflow