У меня есть таблица записей с html-флажком рядом с каждой записью, созданной с помощью for stmt. Текущая функциональность, которая позволяет добавить Sample
к Container
. Я добавил selectall
флажок также через JavaScript
В настоящее время каждая запись также имеет привязку: {% url 'depot:change_container' operation='add' pk=container.container_id fk=unassigned.sample_id %}
для добавления ее в другую таблицу, это происходит через м2м, а другая таблица имеет противоположную функциональность.
Чтобы разрешить отправку нескольких записей, я обертываю таблицу в форму, задайте для действия указанное выше действие, но как передать несколько записей в представлении?
Вот мой код:
Template
<div class="float-left col-md-4">
<h4 class="kap">Samples</h4>
<div class="table-responsive">
<form class="" action="{% url 'depot:change_container' operation='add' pk=container.container_id fk=unassigned.sample_id %}" method="POST">
{% csrf_token %}
<table class="table-striped table-dark">
<thead>
<tr>
<th style="padding-left:5px;">
<input type="checkbox" onclick="toggle(this);" />
</th>
<th></th>
<th>E.N.C.S</th>
<th>Current Location</th>
</tr>
</thead>
<tbody>
{% for unassigned in unassigned_samples %}
<tr>
{% if unassigned not in container_contents %}
<td style="padding-left:5px;"><input type="checkbox" /></td> # the checkbox method
<td style="margin:10px; padding:10px;"><a href="{% url 'depot:change_container' operation='add' pk=container.container_id fk=unassigned.sample_id %}" class="badge badge-primary" role="button"> # the anchor method
<i class="fas fa-arrow-left fa-2x"></i>
</a></td>
<td>{{ unassigned.area_easting }}.{{ unassigned.area_northing }}.{{ unassigned.context_number }}.{{ unassigned.sample_number }}</td>
{% for container in unassigned.containers.all %}
<td>{{ container.location_id }}.{{ container.container_name }}</td>
{% empty %}
<td>None</td>
{% endfor %}
</tr>
{% endif %}
{% empty %}
<tr>
<td>
<p>These are not the samples you are looking for!</p>
<p>Use the above filter to search for a sample.
</p>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="save btn btn-default"><-- Move</button>
</form>
</div>
</div>
</section>
вид
def change_container(request, operation, pk='', fk=''):
container = Container.objects.get(pk=pk)
sample = Sample.objects.get(pk=fk)
if request.method == 'POST': # this is my guess work
id_list = request.POST.getlist('') # this is my guess work
if operation == 'add':
ContainerSamples.add_to_container(container, sample)
elif operation == 'remove':
ContainerSamples.remove_from_container(container, sample)
return redirect('depot:detailcontainer', container_id=pk)