Py学习  »  问与答

Django怎么通过checkbox多选删除数据库数据?

HelloSam • 8 年前 • 7271 次点击  

一下是我模板里checkbox多选删除的 代码:

{% for report_table_context in report_table.values_list %}
        <tr>
            {% for report_table_context_data in report_table_context %}
                {% if forloop.counter != 1 %}
                    <td> {{ report_table_context_data  }}</td>
                {% else %}
                    <td><input type="checkbox"  name="ids" value=report_table_context_data /></td>
                {% endif %}
            {% endfor %}
        </tr>
{% endfor %}

其中 ,当forloop == 1时,report_table_context_data的值是id。所以,该table的第一列 是checkbox。

一下是我view里的代码:

if request.method == 'POST':
    id_list = request.POST.getlist('ids')
    for id in id_list.getvalue:
        RepairReport.objects.filter(report_table_id = id).delete()

请问应该怎么写这些代码?我这样写是实现不了的。而且我不知道id_list有没有getvalue()这个方法……求指教?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1214
 
7271 次点击  
文章 [ 22 ]  |  最新文章 8 年前