本节目标
审核写到数据库,我就单独写一个如下的 页面
单机go后就跳转到如下图界面,我们这节课的目标就是写一个这样的页面
asset\admin.py部分代码 注释如下:
class NewAssetApprovalZoneAdmin(admin.ModelAdmin):
list_display = ('sn','asset_type','manufactory','model','cpu_model','cpu_count','cpu_core_count','ram_size','os_distribution','os_release','date','approved','approved_by','approved_date')
actions = ['approve_selected_objects'] def approve_selected_objects(modeladmin, request, queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
print("----select",selectd) '''
ACTION_CHECKBOX_NAME 是获取选中的id
两个的id都打印出来?为什么是2,checkbox在后台获取的就不是一个了,是一个列表
获取的是一个列表 getlist 4,2 是什么?
就是那个自增ID
''' ct = ContentType.objects.get_for_model(queryset.model)
return HttpResponseRedirect("/asset/new_assets/approval/?ct=%s&ids=%s" % (ct.pk, ",".join(selected)))
approve_selected_objects.short_description = "批准入库" '''在菜单下拉框中显示"批准入库"'''
ACTION_CHECKBOX_NAME是什么?
两个的id都打印出来?为什么是2,checkbox在后台获取的就不是一个了,是一个列表
checkbox在后台获取方式
getlist 4,2 是什么?
获取的就是那个自增ID
为什么要获取这两个东西?
你要跳到一个新的页面,跳转了一个url,你没有办法把对象传过去,所以你要获取这两个东西
数据回报到哪里呢?
1、asset\admin.py文件下的
return HttpResponseRedirect("/asset/new_assets/approval/?ct=%s&ids=%s" % (ct.pk, ",".join(selected)))
2、asset\url.py下的
url(r'^new_assets/approval/$', views.new_assets_approval, name="new_assets_approval"),
3、asset\views.py文件下的
def new_assets_approval(request):
'''是以get还是以post跳转过去的?是以get方式跳转过去的,所以以下代码我们就不用看post代码了,直接看get方法"'''
if request.method == 'POST':
request.POST = request.POST.copy()
approved_asset_list = request.POST.getlist('approved_asset_list')
approved_asset_list = models.NewAssetApprovalZone.objects.filter(id__in=approved_asset_list) response_dic = {}
for obj in approved_asset_list:
request.POST['asset_data'] = obj.data
ass_handler = core.Asset(request)
if ass_handler.data_is_valid_without_id():
ass_handler.data_inject()
obj.approved = True
obj.save() response_dic[obj.id] = ass_handler.response
return render(request, 'assets/new_assets_approval.html',
{'new_assets': approved_asset_list, 'response_dic': response_dic})
else:
ids = request.GET.get('ids')
'''获取到ids,ids是那?是我们刚选中的那几条数据'''
id_list = ids.split(',')
new_assets = models.NewAssetApprovalZone.objects.filter(id__in=id_list) return render(request, 'assets/new_assets_approval.html', {'new_assets': new_assets})
'''
从待批准区里把这几条数据过滤出来返回到这个new_assets_approval.html地址,
另外一个页面就是列出要批准的资产,问你要不要批准
前端页面代码
{% extends 'base.html' %} {% block page-container %}
<div class="eq-height"> <div class="col-sm-4 eq-box-sm"> <!--Panel with Header-->
<!--===================================================-->
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title" style="color: red; ">确认要将下列资产存入数据库么?</h3>
</div>
<div class="panel-body">
<form method="post" action="{% url 'new_assets_approval' %}">{% csrf_token %}
<table class="table table-hover">
<tr>
<th>#</th>
<th>SN</th>
<th>资产类型</th>
<th>厂商</th>
<th>型号</th>
<th>CPU型号</th>
<th>CPU颗数</th>
<th>CPU核数</th>
<th>内存大小</th>
<th>汇报日期</th> </tr>
{% for new_asset in new_assets %}
<tr data-set="{{ new_asset.id }}">
<td>{{ forloop.counter }}</td>
<td>{{ new_asset.sn }}</td>
<td>{{ new_asset.asset_type }}</td>
<td>{{ new_asset.manufactory }}</td>
<td>{{ new_asset.model }}</td>
<td>{{ new_asset.cpu_model}}</td>
<td>{{ new_asset.cpu_count}}</td>
<td>{{ new_asset.cpu_core_count}}</td>
<td>{{ new_asset.ram_size}}</td>
<td>{{ new_asset.date }}</td>
{% if new_asset.id in response_dic %}
<td><a href="#asset_{{ new_asset.id }}">执行结果</a></td>
{% endif %} </tr>
{% endfor %} </table>
<select name="approved_asset_list" class="hide" multiple>
{% for new_asset in new_assets %}
<option value="{{ new_asset.id }}" selected>{{ new_asset.sn }}</option>
{% endfor %}
</select>
<input type="submit" value="Yes, I'm sure" class="btn btn-danger"> </form> <div >
{% for key,msg in response_dic.items %}
<div>
{% for asset in new_assets %}
{% if asset.id == key %}
<h4 id="asset_{{ asset.id }}"><a style="color:deepskyblue" href="/admin/assets/asset/?q={{ asset.sn }}" target="_blank">{{ asset.sn }} </a> | {{ asset.manufactory }} {{ asset.model }} | {{ asset.cpu_model }} </h4>
{% endif %}
{% endfor %}
<p style="color: green">
{% for item in msg.info %}
{{ item |safe}}
<br/>
{% endfor %}
</p>
<p style="color: red">
{% for item in msg.error %} {% if 'Warning' in item.keys.0 %}
<span style="color:orange;">{{ item }}</span>
{% else %}
{{ item }}
{% endif %}
<br/>
{% endfor %}
</p>
</div>
<hr/>
{% endfor %}
</div> </div> </div>
<!--===================================================-->
<!--End Panel with Header-->
<button id="scroll-top" class="btn"><i class="fa fa-chevron-up"></i></button> </div> </div> {% endblock %}