使用Django更新数据时如何获取多个数据?

2024-10-02 12:36:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用多个复选框更新数据 这是我的视图.py你知道吗

def update_kel_stat(request, id):
if request.method == "POST":
    cursor = connection.cursor()
    sql = "UPDATE keluargapeg_dipkeluargapeg SET KelStatApprov='3' WHERE (PegUser = %s )" % id 
    cursor.execute(sql)

这是我的模板.html你知道吗

<form method="post" action="" name="kel" enctype="multipart/form-data">
{% for keluarga in kels %}
    <tr id="{{ keluarga.KelID }}">
        <td>
            <a href="#">{{ keluarga.KelNamaLengkap }}</a>
        </td>
        <td>{{ keluarga.KelStatApprov }}</td>
        <td>{{ keluarga.KelKetRevisi }}</td>
        <td><input type="checkbox" name="kel[]"
                   value="{{ keluarga.KelID }}"></td>
    </tr>
{% endfor %}
<tr>
    <td>
        <button type="button" name="btn_delete" id="btn_delete"
                class="btn btn-success"
                onClick="setDeleteAction();">Approve
        </button>
    </td>
</tr>
</form>

如何从中的复选框中获取多个值模板.html去django查看.py?你知道吗


Tags: namepyformidsqlrequestbuttoncursor
1条回答
网友
1楼 · 发布于 2024-10-02 12:36:07

首先,对于这样简单的查询,不建议使用原始SQL。使用django ORM更新某些记录非常容易:

Entry.objects.filter(id=10).update(comments_on=False)

至于你的问题,你可以这样做。在模板中:

{% for keluarga in kels %}
    <input type="checkbox" name="kel" id="kel{{ forloop.counter }}" value="{{ keluarga.KelID }}">
    <label for="kel{{ forloop.counter }}">Choice {{ forloop.counter }}</label>
{% endfor %}

在你看来:

kels = request.POST.getlist('kel')
Kel.objects.filter(id__in=kels).update(StatApprov=3)

相关问题 更多 >

    热门问题