一括削除 #django

一括削除

一括削除のurls.py

testapp/views/urls.py

urlpatterns = [
    #...
    path('batch_delete', UserView.batch_delete_view, name='batch_delete'),
]

一括削除のtemplate

testapp/templates/batch_delete.html

      <!-- 一括処理の場合はformはusersの外側へ -->
      <form method="post" action="update/{{ user.id }}/">
        {% csrf_token %}
        {% for user in users %}
          <tr>
            <td>
              {{ user.id }}
            </td>
            <td>
              <label>{{ user.name }}</label>
            </td>
            <td>{{ user.created_at }}</td>
            <td>{{ user.updated_at }}</td>
            <td>
              <label><input type="checkbox" name="batch_ids[]" value="{{ user.id }}">{{ user.id }}</label>
              <input type="submit" name="batch_delete" value="一括削除" />
            </td>
          </tr>
        {% endfor %}
      </form>
      <form method="post" action="insert_from_batch_delete">
        {% csrf_token %}
        名前 <input type="textbox" value="" name="username" />
        <input type="submit" name="insert" value="登録" />
      </form>

一括削除の表示のview

testapp/views/user_view.py

    def batch_delete_view(request):
        users = {
            'users': AppUser.objects.all(),
        }

        return render(request, 'batch_delete.html', users)

一括削除の削除処理のview

testapp/views/user_view.py

    def batch_delete(ids):
        AppUser.objects.filter(id__in=ids).all().delete()

        return HttpResponseRedirect('/')

END

Close