django - django,使用复选框传递多条记录
问题描述
我有一个记录表,在通过 for stmt 创建的每条记录旁边都有一个 html 复选框。我当前的功能允许将单个Sample
添加到Container
. 我selectall
还通过 javascript 添加了一个复选框
目前,每条记录也有一个锚点:{% url 'depot:change_container' operation='add' pk=container.container_id fk=unassigned.sample_id %}
要将其添加到另一个表,这通过 m2m 进行,另一个表具有相反的功能。
为了允许发送多条记录,我将表格包装在一个表单中,将操作设置为上述操作,但是如何在视图中传递多条记录?
到目前为止,这是我的代码:
模板
<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)
解决方案
你猜对了:)
只需为清单提供一个名称属性 ex:name ="checks[]"
在 html 中
检索相同的视图:some_var = request.POST.getlist('checks[]')
这里:
<thead>
<tr>
<th style="padding-left:5px;">
<input type="checkbox" name ="checks[]" onclick="toggle(this);" />
</th>
<th></th>
<th>E.N.C.S</th>
<th>Current Location</th>
</tr>
</thead>
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('checks[]') # this is my guess work
推荐阅读
- reactjs - 通过 ui 触发动作的行为与使用键事件处理时不同
- flutter - CheckBox - 完成该商店的工作后,在商店列表上显示一个勾号
- app-actions - 有没有办法在控制台中了解我的应用操作审核状态?
- java - 从 URL 下载 pdf 文件并将其保存在 android (java) 的特定文件夹中
- express - Nuxt/Express API 在控制台而不是浏览器中返回
- javascript - 未捕获的 ReferenceError:我的函数未在 HTMLInputElement.onclick 中定义
- html - CSS嵌套类不能直接选择?
- ionic-framework - 单击按钮后无法路由到下一页
- excel - 需要在创建后立即激活文本框而不调用其名称
- mongodb - MongoDB 从其他集合中获取数组中最常用的类别以及计数