django,使用复选框传递多个记录

时间:2019-05-12 10:07:18

标签: django checkbox

我有一个记录表,在通过for stmt创建的每个记录旁边都有一个html复选框。我当前的功能是允许将单个Sample添加到Container。我还通过JavaScript添加了selectall复选框

当前每个记录还具有一个锚点:{% 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)

1 个答案:

答案 0 :(得分:0)

您猜对了:)
只需在html
中将名称属性赋予清单{:name ="checks[]" 在视图中检索相同的内容: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