Python,Django:如何将已检查的 table 行放入列表中?
Python, Django: how to get checked table rows into lists?
晚上好,
我希望有人知道我将 table 中的所有选中行放入多维列表(提交后)的问题的答案?
我知道我可以做这样的事情来获取——例如——所有检查过的 ID 到一个列表中:
模板:
<form action="" method="post">
{% csrf_token %}
<div class="table-wrapper">
<table class="table table-hover">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">id</th>
<th scope="col">country</th>
<th scope="col">continent</th>
<th scope="col">...</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>
<input type="checkbox" name="list_gap" value="{{ item.id }}">
</td>
<td>{{ item.id}}</td>
<td>{{ item.country }}</td>
<td>{{ item.continent}}</td>
<td>...</td>
views.py
if request.method == 'POST':
checked_data = request.POST.getlist('list_gap')
但是如前所述,这只会给我一个已检查 ID 的列表!有没有办法把所有行的数据放到一个多维列表中,比如like..?
list_checked = [
[1, 'country', 'continent', '...'],
[2, 'country', 'continent', '...'],
[3, 'country', 'continent', '...'],
...
]
感谢您的帮助,祝您晚安!
我建议创建一个查询集以从数据库中提取实例。
checked_data = request.POST.getlist('list_gap')
checked_instances = Model.objects.filter(id__in=checked_data)
晚上好,
我希望有人知道我将 table 中的所有选中行放入多维列表(提交后)的问题的答案?
我知道我可以做这样的事情来获取——例如——所有检查过的 ID 到一个列表中:
模板:
<form action="" method="post">
{% csrf_token %}
<div class="table-wrapper">
<table class="table table-hover">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">id</th>
<th scope="col">country</th>
<th scope="col">continent</th>
<th scope="col">...</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>
<input type="checkbox" name="list_gap" value="{{ item.id }}">
</td>
<td>{{ item.id}}</td>
<td>{{ item.country }}</td>
<td>{{ item.continent}}</td>
<td>...</td>
views.py
if request.method == 'POST':
checked_data = request.POST.getlist('list_gap')
但是如前所述,这只会给我一个已检查 ID 的列表!有没有办法把所有行的数据放到一个多维列表中,比如like..?
list_checked = [
[1, 'country', 'continent', '...'],
[2, 'country', 'continent', '...'],
[3, 'country', 'continent', '...'],
...
]
感谢您的帮助,祝您晚安!
我建议创建一个查询集以从数据库中提取实例。
checked_data = request.POST.getlist('list_gap')
checked_instances = Model.objects.filter(id__in=checked_data)