点击Go跳转到之前的删除页面
1、先生成输入框和GO,和生成单选框,table_objs.html
<div class="row" style="margin-top: 5px;margin-left: 0">
<form onsubmit="return ActionSubmit(this)" method="post">{% csrf_token %}
<div class="col-lg-2">
<select class="form-control" id="action_list" name="action">
<option value="" name="">----------</option>
{% for action in admin_class.actions %}
<option value="{{action}}" name="">{% get_action_verbose_name admin_class action%}</option>
{% endfor %}
</select>
</div>
<div class="col-lg-1">
<button type="submit" class="btn">Go</button>
</div>
</form>
</div>
2、单选框和多选框按钮,表单提交的处理table_objs.html
<script>
function CheckAllToggle(ele) {
// console.log($(ele).prop("checked"))
// console.log($("input[tag='obj_checkbox']"))
// var wait_checked = $("input[tag='obj_checkbox']")
if ($(ele).prop("checked")){
$("input[tag='obj_checkbox']").prop('checked',true);
}else{
$("input[tag='obj_checkbox']").prop('checked',false);
}
}
function CheckOneTaggle() {
var wait_checked = $("input[tag='obj_checkbox']")
$(wait_checked).each(function () {
if(!($(this).prop("checked"))){
$("input[id='ckbox']").prop("checked",false);
}else{
if($(wait_checked).prop("checked")){
$("input[id='ckbox']").prop("checked",true);
}
}
})
// var count = 0
// console.log(wait_checked)
// console.log($(ele).length)
// console.log($(ele).prop("checked"))
// if (!($(ele).prop("checked"))){
// $("input[id='ckbox']").prop("checked",false)
// }else{
// count+=1
// if(count==wait_checked){
// $("input[id='ckbox']").prop("checked",true)
// }
// }
}
function ActionSubmit(form_ele) {
//selected_action
//单多选框
var selected_ids=[];
$("input[tag='obj_checkbox']:checked").each(function () {
selected_ids.push($(this).val());
})
var selected_action=$("#action_list").val();
if (selected_ids.length==0){
alert("No object got selected!");
return
}
//上面的action选择框select
if (!selected_action){
alert("No action got selected!");
return
}
//start submit
var selected_ids_ele = "<input name='selected_ids' type='hidden' value='" + selected_ids.toString() + "' >"
$(form_ele).append(selected_ids_ele);
return true
}
</script>
3、在view.py判断前端选择checkbox的数据,然后处理调用kind_admin.py里面的selected_ids_ele函数:
def display_table_objs(request,app_name,table_name):
"""搜索、分页、排序页面"""
print("-->",app_name,table_name)
#models_module = importlib.import_module('%s.models'%(app_name))
#model_obj = getattr(models_module,table_name)
admin_class = kind_admin.enabled_admins[app_name][table_name]
#admin_class = king_admin.enabled_admins[crm][userprofile]
if request.method == 'POST': #action来了
action = request.POST.get("action")
selected_ids = request.POST.get("selected_ids")
if selected_ids:#filter
selected_ids = admin_class.model.objects.filter(id__in=(selected_ids.split(',')))
else:
raise KeyError("no object select")
if hasattr(admin_class,action):
#获取admin_class里面的delete_selected_obj函数
action_func = getattr(admin_class,action)
#在请求里面手动添加action为在删除提交时有action
request._action = action
#返回调用
return action_func(admin_class,request,selected_ids)
4、在kind_admin.py增加action=['delete_selected_objs']跳转到删除页面kind_admin.py,需要把上面的action,selected_ids_ele作为请求数据:
class BaseAdmin(object):
list_display = []
list_filter = []
list_per_page = 3
search_fields = ['qq','name']
ordering = None
filter_horizontal = []
actions = ["delete_selected_objs"]
def delete_selected_objs(self,request,querysets):
"""
#self: crm.CustomerAdmin
:param arg1:arg1: <WSGIRequest: POST '/admin/crm/customer/'>,request
:param arg2:<QuerySet [<Customer: 38399398@qq.com>
:return:
"""
print("delete_selected_objs",self,request,querysets)
selected_ids = ','.join([str(i.id) for i in querysets])
app_name = self.model._meta.app_label
table_name = self.model._meta.model_name
if request.POST.get("delete_confirm") == "yes":
querysets.delete()
return redirect("/kind_admin/%s/%s"%(app_name,table_name))
return render(request,"kindadmin/table_objs_delete.html",{"admin_class":self,
"objs":querysets,
"app_name":app_name,
"table_name":table_name,
"selected_ids":selected_ids,
"action":request._action})
delete_selected_objs.display_name = "删除多选数据" #为了在select里面显示中文
5、table_objs_delete.html删除提交,由于提交时找不到前面请求过来的action、selected_ids,会在页面增加两个hidden的标签获取他们两个的值,删除提交的时候前面也有post请求导致区分不了到底是删除还是其他,所以也增加了一个隐藏的input标签,delete_selected_objs函数会先判断如果请求过来的delete_confirm=yes那么就是删除的请求:
table_objs_delete.html:
{% extends "kindadmin/table_index.html"%}
{% load tags %}
{% block container%}
{% display_obj_related objs %}
<form method="post">{% csrf_token %}
<input type="submit" class="btn btn-danger" value="Yes,I SURE">
<input type="hidden" value="yes" name="delete_confirm">
<input type="hidden" name="selected_ids" value="{{selected_ids}}">
<input type="hidden" name="action" value="{{action}}">
<a class="btn btn-info" href="{% url 'table_objs' app_name table_name %}">NO,Take Me Back</a>
</form>
{% endblock%}