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:
template:
<form action="" method="post">
{% csrf_token %}
<div class="table-wrapper">
<table class="table table-hover">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">id</th>
<th scope="col">country</th>
<th scope="col">continent</th>
<th scope="col">...</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>
<input type="checkbox" name="list_gap" value="{{ item.id }}">
</td>
<td>{{ item.id}}</td>
<td>{{ item.country }}</td>
<td>{{ item.continent}}</td>
<td>...</td>
views.py
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!
Advertisement
Answer
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)