1

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!

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.