У меня есть список (в виде таблицы) некоторых данных, у меня есть кнопка в каждой строке, которая через представление назначает запись (образец) другой записи (контейнеру) через m2m <a href="{% url 'depot:change_container' operation='add' pk=container.container_id fk=unassigned.sample_id %}" class="badge badge-primary" role="button">
. Это работает. Меня попросили добавить опцию 'checkall', я добился этого с помощью javascript. Я вложил код в форму.
Так как мне передать несколько записей (а не только 1) в Django из флажков javascript, чтобы любые проверенные образцы были назначены их контейнеру?
# javascript
<script type="text/javascript">
function toggle(source) {
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i] != source)
checkboxes[i].checked = source.checked;
}
}
</script>
Шаблон:
<div class="float-left col-md-4">
<h4 class="kap">Unassigned Samples</h4>
<div class="table-responsive">
<form class="" action="" method="post">
<table class="table-striped table-dark">
<thead>
<tr>
<th style="padding-left:5px;">
<input type="checkbox" onclick="toggle(this);" />
</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>
<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">
<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">Save</button>
</form>
</div>
</div>
Представление (которое содержит элемент фильтра (поиска))
def detailcontainer(request, container_id):
container = get_object_or_404(Container, pk=container_id)
samples = container.samples.all()
container_contents = container.samples.all()
unassigned_samples = Sample.objects.all()
unassigned_samples2 = Sample.objects.all()[:10]
qs = Sample.objects.all()
easting_query = request.GET.get('area_easting')
northing_query = request.GET.get('area_northing')
context_query = request.GET.get('context_number')
sample_number_query = request.GET.get('sample_number')
sample_type_query = request.GET.get('sample_type')
if (
easting_query == '' or easting_query is None and
northing_query == '' or northing_query is None and
context_query == '' or context_query is None and
sample_number_query == '' or sample_number_query is None and
sample_type_query == '' or sample_type_query is None
):
unassigned_samples = unassigned_samples.none()
if easting_query != '' and easting_query is not None:
unassigned_samples = unassigned_samples.filter(area_easting__icontains=easting_query)
if northing_query != '' and northing_query is not None:
unassigned_samples = unassigned_samples.filter(area_northing__icontains=northing_query)
if context_query != '' and context_query is not None:
unassigned_samples = unassigned_samples.filter(context_number__icontains=context_query)
if sample_number_query != '' and sample_number_query is not None:
unassigned_samples = unassigned_samples.filter(sample_number__icontains=sample_number_query)
if sample_type_query != '' and sample_type_query is not None:
unassigned_samples = unassigned_samples.filter(sample_type__icontains=sample_type_query)
qs = qs
context = {
'queryset': qs,
'container':container,
'container_contents': container_contents,
'unassigned_samples': unassigned_samples,
'easting_query': easting_query,
'northing_query': northing_query,
'context_query': context_query,
'sample_number_query': sample_number_query,
'sample_type_query': sample_type_query
}
return render(request, 'container/detailcontainer.html', context)