views.py 代码:
from django.shortcuts import render, redirect from .models import YourModel # 使用你实际的模型名称替换 def batch_delete(request): if request.method == 'POST': ids = request.POST.get('ids') if ids: id_list = ids.split(',') YourModel.objects.filter(id__in=id_list).delete() return redirect('your_model_list_view') # 使用你实际的视图名称替换 else: return redirect('your_model_list_view')
HTMl 页面:
<form method="post"> {% for item in object_list %} <input type="checkbox" name="ids" value="{{ item.id }}"> {{ item.name }} <br> {% endfor %} <input type="submit" value="删除选中项"> </form>
urls.py:
path('batch_delete/',views.batch_delete),
标签:redirect,批量,request,list,ids,django,页面,id,delete From: https://www.cnblogs.com/p4567/p/17704917.html