首页 > 解决方案 > 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)

标签: djangocheckbox

解决方案


你猜对了:)
只需为清单提供一个名称属性 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


推荐阅读