标签:发放 继承 char actions trap 方便 contain href red
https://www.hoboes.com/Mimsy/hacks/django-actions-their-own-intermediate-page/
class 模型管理类(admin.ModelAdmin): actions = [‘自定义方法名‘] #下面以行政部分发放多个办公用具给某领用人的操作来说明 #定义一个选择员工的页面form,将来在动作中选择执行时会跳转到模板页面,同时填写参数,这里是选择员工 class StaffSelectForm(forms.Form): #下面是获取选择的记录 _selected_action = forms.CharField(widget=forms.MultipleHiddenInput) #这里是将来要选择的员工处,本方法就是要通过它传参 _staff = forms.ModelChoiceField(Staff.objects, label=‘员工‘) def 自定义方法名(self, request, queryset): form = None print(request.POST) if ‘cancel‘ in request.POST: self.message_user(request, ‘设备出库取消‘) return elif ‘_staff‘ in request.POST: form = self.StaffSelectForm(request.POST) if form.is_valid(): #这里从form中获取到传过来的参数_staff staff = form.cleaned_data["_staff"] print(request.POST.getlist(admin.helpers.ACTION_CHECKBOX_NAME)) for q in queryset: #这里对要进行的操作自定义编写 #我这里是把选择的记录当前领取人作了修改 q.owner = staff q.save() self.message_user(request, f‘{staff}成功领取{queryset.count()}台设备‘) return HttpResponseRedirect(request.get_full_path()) if not form: #这里是点击执行自定义动作时初始化form表单 #把选择的记录值传过去前端页面 form = self.StaffSelectForm(initial={‘_selected_action‘: request.POST.getlist(admin.helpers.ACTION_CHECKBOX_NAME)}) return render( request, ‘APP名称/对应的自定义模板.html‘, {‘devs‘: queryset, ‘form‘: form, ‘path‘:request.get_full_path(), ‘action‘: ‘自定义方法名‘, ‘title‘: u‘文具批量领取‘} ) 自定义方法名.short_description = "批量领取"
2、对应在的自定义动作模板:modify_owner.html
#继承默认模板,统一样式 {% extends "admin/base_site.html" %} {% block extrahead %} {{ block.super }} <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> <style> .div_content { width: 100%; text-align: center; } #自定义样式 </style> {% endblock %} {% block content %} <div class=‘container‘> <div class="row"> <form method="POST", action="{{ path }}"> {% csrf_token %} {{ form.as_p }} <!-- 下面是手动显示字段,方便精准控件显示 --> <!-- {% for field in form %} <div class="row margin-top"> <div class="col-md-6 text-right">{{ field.label_tag }}</div><div class="col-md-6 text-left">{{ field }}</div> </div> {% endfor %} --> <p class="row text-center margin-top" style="padding-left: 150px;"> <input type="hidden" name="action" value="{{ action }}" /> <input type="submit" class="btn btn-info btn-sm" name="ok" value="确 认" /> <input type="submit" class="btn btn-info btn-sm margin-left" name="cancel" value="取 消" /> </p> </form> </div> #以下是非关键代码,我这里是列出选择了哪些项,所以后端传过来的参数“devs” <div class="row"> <div class="col-md-3 col-md-offset-6"> <div class="text-center"> <p style="font-size: 14px; font-weight: bolder;">出库设备信息所下:</p> </div> <ul> {% for d in devs %} <li>{{ d.devNO }} 当前归属:{{ d.owner }}</li> {% endfor %} </ul> </div> </div> </div> {% endblock %}
标签:发放 继承 char actions trap 方便 contain href red
原文地址:https://www.cnblogs.com/ameile/p/13858268.html