【问题标题】:Passing checkboxes values to view in django传递复选框值以在 django 中查看
【发布时间】:2015-09-01 14:14:16
【问题描述】:

我有一个包含一些数据库数据的表

<table id="id_list_table" class="table table-condensed">
<caption>Inputs list</caption>
    <thead>
      <tr>
        <th>#</th>
        <th id="input">Input</th>
      </tr>
    </thead>
    <tbody id="fbody">
    {%for input in InputsAll%}
        <tr>
            <td>{{ input.input }}</td>
            <td>
                <a class="btn btn-primary btn-xs" href="{% url 'edit' %}?input_num={{input.id}}" id="edit">edit</a>
                <a class="btn btn-danger btn-xs" href="{% url 'delete' %}?input_num={{input.id}}" id="remove">remove</a>
                <a class="btn btn-success btn-xs" href="{% url 'resolve' %}?input_num={{input.id}}"id="resolve">resolve</a>
                <input type="checkbox" name="inputs" id="option{{input.id}}" value={{input.id}} />
            </td>
        </tr>
    {%endfor%}
    </tbody>
</table>

我想为每个字段添加一个复选框并传递检查数据的 id 以查看和进行组操作,而不是一个一个地删除项目。 我添加了这个复选框

<input type="checkbox" name="inputs" id="option{{input.id}}" value={{input.id}} />

我应该如何传递所有检查的值来查看?是这样的吗?

【问题讨论】:

  • 你可能需要把它放在一个表单标签中,并确保它是通过 POST 发送的。
  • @cdvv7788 我发现了这个问题,但我不确定他是如何传递数据来查看的。我应该在表单标签中添加什么?所有表格或只是复选框输入?
  • 首先阅读一些有关表单的信息 (w3schools.com/html/html_forms.asp)。提交表单时,每个输入都通过选定的方法发送,因此如果要发送列表,则需要添加所有复选框。

标签: django checkbox django-templates django-views


【解决方案1】:

views.py

if request.method == 'POST':
        #gives list of id of inputs 
        list_of_input_ids=request.POST.getlist('inputs')

希望这可以解决您的大部分问题。查看此链接 Checkboxes for a list of items like in Django admin interface

【讨论】:

  • 感谢您的评论。但我不完全明白应该如何触发删除。
  • 一旦你得到了 object.id,你就可以在 list_of_inputs_ids: y = Model.objects.get(pk=x) y.delete() 中为 x 做一个组操作
猜你喜欢
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多