2👍
Okay, let’s go through this step by step.
1) You want the Agent_Delete function to receive data from the frontend (a list of IDs for the objects to be deleted). So you need to register the function with a url in urls.py (I am assuming you have done this already).
This should something like the following in urls.py:
from .views import *
urlpatterns = [
url(r'^delete_agent/$', Agent_Delete),
... #Along with other urls
]
2) From the front-end you want to POST the data to the view you have created above. To POST data you need a form. So, encapsulate the table in a form. It should look something like:
<form action="{% url 'delete_agent'%}" method="POST">
{% csrf_token %} <- You will need to pass this token
<!--Table with checkboxes goes here-->
...
</form>
Now, you have a form which will try to POST the input to a particular URL which is mapped to a view. Let’s handle the view.
@csrf_exempt #Add this too.
def Agent_Delete(request, id=None):
if request.method == 'POST': #<- Checking for method type
id_list = request.POST.getlist('instance')
#This will submit an array of the value attributes of all the
#checkboxes that have been checked, that is an array of {{obj.id}}
# Now all that is left is to iterate over the array fetch the
#object with the ID and delete it.
for agent_id in id_list:
Agent.objects.get(id=agent_id).delete()
Source:stackexchange.com