我将在Django中实现多删除(通过选择框)视图。
我知道django.contrib.admin.actions中有一个视图,但我无法将其移植到前端。
我正在寻找一个例子(对于Django视图+ HTML),但找不到任何。
1)如何使用列表页面中的复选框和单个删除按钮创建多个删除对象。 2)列表,删除视图和list.html页面有哪些更改
views.py
def Agent_List(request, id=None): #list items
queryset = Agent.objects.order_by('id')
#queryset = Agent.objects.all()
query = request.GET.get('q')
if query:
queryset=queryset.filter(
Q(Aadhaar_Number__icontains=query) |
Q(PAN_Number__icontains=query) |
Q(Account_Number__icontains=query)
).distinct()
context = {
"object_list": queryset,
"Full_Name ": "Agent_List",
}
return render(request, "customer/Agent_List.html", context)
def Agent_Delete(request, id=None):
instance = get_object_or_404(Agent, id=id)
instance.delete()
messages.success(request, "Successfully deleted")
return redirect("customer:Agent_List")
list.html
{% extends "layout.html" %}
{% load staticfiles %}
<head>
</head>
<body>
<div id="wrapper">
<div id="page-wrapper" class="gray-bg">
{% block content %}
<div class="row wrapper border-bottom white-bg page-heading">
<div class="col-lg-10">
<h2>Agent </h2>
<ol class="breadcrumb">
<li><a href="index.html">Manage My Wallet</a></li>
<li><a>Agent Management </a></li>
</ol>
</div>
<div class="col-lg-2"></div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading">
Agent Profile List
</div>
<div class="panel-body">
<div>
<form method='GET' action=''>
<input type='text ' name='q' placeholder='search' value="{{request.GET.q}}"/>
<input type='submit' value=' Search '/>
</form>
<table class="table" >
<thead>
<tr>
<th></th>
<th>ID</th>
<th data-hide="phone">Full Name</th>
<th data-hide="phone">Agency Name</th>
<th data-hide="phone">Date Of Birth</th>
<th data-hide="phone">Agency Code</th>
<th data-hide="phone">Agent Status </th>
<th class="text-right">Action</th>
</tr>
</thead>
{% for obj in object_list %}
<tbody>
<tr>
<td><input type="checkbox" name="instance" value="{{obj.id}}" >
<td>
<a href='#' data-toggle="collapse" value="{{obj.id}}">
{{ obj.id }}
</td>
<td><a href='#demo' data-toggle="collapse">{{ obj.Full_Name }}</a></td>
<td><a href="#demo" data-toggle="collapse">{{ obj.Agency_Name}}</a></td>
<td><a href="#demo" data-toggle="collapse">{{ obj.Date_of_Birth}}</a></td>
<td><a href="#demo" data-toggle="collapse">{{ obj.Agency_Code}}</a></td>
<td><a href="#demo" data-toggle="collapse"><span class="label label-primary">{{ obj.Agent_Status}}</span></a></td>
<td class="text-right">
<!-- <div class="btn-group">
<a href='{{ obj.get_absolute_url }}' class="badge badge-warning" role="button">View</a>
<a href="{% url 'customer:Agent_Edit' obj.id %}" class="badge badge-info" role="button">Edit</a>
<a href="{% url 'customer:Agent_Delete' obj.id %}" class="badge badge-danger" role="button">Delete</a>
</div> -->
<a href='{{ obj.get_absolute_url }}' class="btn btn-warning btn-circle" type="button"><i class="fa fa-list-alt" aria-hidden="true"></i></a>
<a href="{% url 'customer:Agent_Edit' obj.id %}" class="btn btn-info btn-circle" type="button"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
<a href="{% url 'customer:Agent_Delete' obj.id %}" class="btn btn-danger btn-circle" type="button"><i class="fa fa-trash" aria-hidden="true"></i></a>
</td>
</tr>
</tbody>
{% endfor %}
<tfoot>
<tr>
<td colspan="7">
<ul class="pagination pull-right"></ul>
<input type="submit" name="delete" value="Delete Items" />
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<div id="demo" class="collapse" value="{{obj.Full_Name}}">
<div class=" col-md-5 col-lg-5 ">
{% for obj in object_list %}
<table class="table table-user-information">
<tbody>
<tr>
<td> Full Name :- </td>
<td>{{ obj.Full_Name }}</td>
</tr>
<tr>
<td>Date of Birth :-</td>
<td>{{ obj.Date_of_Birth }}</td>
</tr>
<tr>
<td>Phone Number:-</td>
<td>{{ obj.Phone_Number }}</td>
</tr>
<tr>
<td>Account Number :- </td>
<td>{{ obj.Account_Number}} </td>
</tr>
<tr>
<td>PAN Number :- </td>
<td>{{ obj.PAN_Number }} </td>
</tr>
<tr>
<td>Aadhaar Number :-</td>
<td>{{ obj.Aadhaar_Number }}</td>
</tr>
<tr>
<td>Agent Name :- </td>
<td>{{ Agent_Name }}</td>
</tr>
<tr>
<td>Agency Name :- </td>
<td>{{ Agency_Name }} </td>
</tr>
<tr>
<td>Agency Code :- </td>
<td>{{ Agency_Code }} </td>
</tr>
<tr>
<td>Agent Status :- </td>
<td>{{ Agent_Status }} </td>
</tr>
</tbody>
</table>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="footer" >
<div><strong>Copyright</strong></div>
</div>
</div>
</div>
</body>
请告诉提示
答案 0 :(得分:0)
好的,我们一步一步来看看。
1)您希望Agent_Delete函数从前端接收数据(要删除的对象的ID列表)。所以你需要在urls.py中使用url注册该函数(我假设你已经这样做了)。
这应该类似于urls.py中的以下内容:
<h1 id="xhide">Hello World</h1>
2)从前端开始,您希望将数据发布到您在上面创建的视图。要POST数据,您需要一个表单。因此,将表封装在表单中。它应该看起来像:
from .views import *
urlpatterns = [
url(r'^delete_agent/$', Agent_Delete),
... #Along with other urls
]
现在,您有一个表单,它将尝试将输入POST到映射到视图的特定URL。让我们处理视图。
<form action="{% url 'delete_agent'%}" method="POST">
{% csrf_token %} <- You will need to pass this token
<!--Table with checkboxes goes here-->
...
</form>