如何将输入标签的选定复选框 ID 传递给 Django 中的视图并批量更新表中的选定行

  • 本文关键字:Django 视图 更新 标签 ID 复选框 django
  • 更新时间 :
  • 英文 :


** 1.如何将输入标签的选定复选框 ID(在 for 循环中,因此输入标签的名称和值是动态的(传递给 Django 中的视图并批量更新 Django 中表的选定行**

<div class="jumbotron">
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<input  class = "btn btn-primary" type="submit" value="Search">
</form>
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td>  {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td>  {{   value.name }}  </td>

</tr>
{% endfor %}
</tbody>
</table>

**意识到由于我没有在表单标签中保留输入标签,因此请求不会捕获输入标签的名称值。POST(现在更改在 html 中如下所示(,稍后在 django 视图中,我使用 dict.pop(( 删除了不必要的键值,并在 django orm 中创建了一个包含主键
的列表(model.filter(id__in= lst_of_id_captured(.update(field='somevalue'( **

<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td>  {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td>  {{   value.name }}  </td>

</tr>
{% endfor %}
</tbody>
</table>
<input  class = "btn btn-primary" type="submit" value="Search">
</form>

最新更新