1.摘要
近期有关于建立简单设备管理系统的需求,结合Django学习,在参考众多大神来源的资源基础上,参考刘江博客,实现了基本功能,并增加了用户登陆、增删改、打印、导出、copy功能。
主要参考资源:
(1)刘江Django教程
(2)Python Django 的学习路径
(3)Django学习专题
(4)谢育政的博客
(5)基于 Python 3.5 + Django 2.0 开发的运维管理系统
2.开发环境
Django 2.1.3+AdminLTE+Datatable
3.开发过程
3.1基本框架实现
根据刘江博客CMDB之资产管理系统教程,按照models数据结构、views视图函数和template模板的顺序进行构建,形成一个MTV的完整结构。因为教程已经十分详细,有兴趣练手可直接看,这里区别一下2.0版本后不再使用url()函数,改用path()函数,在编写url.py文件时注意修改。
login界面:
index界面:
index.PNG
dashboard界面:
dashboard.PNG
3.2index页面“操作”栏
在index.html中,加入列数据,在其中调用add,edit和del_equipment页面。
<td>
<a href="{% url 'assets:add' %}" title="添加"><i class="glyphicon glyphicon-plus"></i></a>
<a href="{% url 'assets:edit' asset.id %}" title="修改"><i class="glyphicon glyphicon-pencil"></i></a>
<a href="{% url 'assets:del_equipment' asset.id %}" onclick="return delete_data()" title="删除"><i class="glyphicon glyphicon-trash text-danger"></i></a>
<script language="JavaScript">
function delete_data() {
if(confirm('您确定要删除吗?')){
return true;
}else{
return false;
}
}
</script>
</td>
(1)add
构建此功能,需要三步,由于要向后台提交数据,这里第一步在forms.py定义RegisterForm:
class RegisterForm(forms.Form):
asset_type_choice = (
('server', '服务器'),
('networkdevice', '网络设备'),
('storagedevice', '存储设备'),
('securitydevice', '安全设备'),
('software', '软件资产'),
)
asset_status = (
(0, '在线'),
(1, '下线'),
(2, '未知'),
(3, '故障'),
(4, '备用'),
)
asset_type = forms.ChoiceField(label='设备类型',choices=asset_type_choice)
name = forms.CharField(label='设备名称',max_length=128,widget=forms.TextInput(attrs={'class':'form-control'}))
sn = forms.FloatField(label='序列号', widget=forms.TextInput(attrs={'class': 'form-control'}))
manufacturer = forms.CharField(label='生产厂家', max_length=256, widget=forms.TextInput(attrs={'class': 'form-control'}))
status = forms.ChoiceField(label='设备状态',choices=asset_status)
idc = forms.CharField(label='机房', max_length=256, widget=forms.TextInput(attrs={'class': 'form-control'}))
purchase_day = forms.DateField(label='购买时间', widget=forms.TextInput(attrs={'class': 'form-control'}))
第二步在view.py添加add函数:
def add(request):
register_form = forms.RegisterForm(request.POST)
if request.method == 'POST':
# register_form = forms.RegisterForm(request.POST)
message = '请检查填写内容'
if register_form.is_valid():
asset_type = register_form.cleaned_data['asset_type']
name = register_form.cleaned_data['name']
sn = register_form.cleaned_data['sn']
manufacturer = register_form.cleaned_data['manufacturer']
status = register_form.cleaned_data['status']
idc = register_form.cleaned_data['idc']
purchase_day = register_form.cleaned_data['purchase_day']
same_sn = models.Asset.objects.filter(sn=sn)
if same_sn:
message = "该设备已存在"
return render(request, 'assets/add.html', locals())
new_equipment = models.Asset()
new_equipment.asset_type = asset_type
new_equipment.name = name
new_equipment.sn = sn
new_equipment.manufacturer = manufacturer
new_equipment.status = status
new_equipment.idc = idc
new_equipment.purchase_day = purchase_day
new_equipment.save()
message = '添加成功'
return redirect('/assets/index/')
return render(request,'assets/add.html',locals())
第三步在url.py中定义add功能的页面链接:
path('add/', views.add, name="add")
第四步在template中定义add.html,具体效果为:
add.PNG
(2)edit
区别于add,edit首先要将该行数据调用显示出来,在进行修改提交,所以要获取asset_id,views.py中添加函数:
def edit(request,asset_id):
edit_form = forms.EditForm(request.POST)
if request.method == 'POST':
if edit_form.is_valid():
asset_type = edit_form.cleaned_data['asset_type']
name = edit_form.cleaned_data['name']
sn = edit_form.cleaned_data['sn']
manufacturer = edit_form.cleaned_data['manufacturer']
status = edit_form.cleaned_data['status']
idc = edit_form.cleaned_data['idc']
purchase_day = edit_form.cleaned_data['purchase_day']
alt = models.Asset.objects.get(id=asset_id)
alt.asset_type = asset_type
alt.name = name
alt.sn = sn
alt.manufacturer = manufacturer
alt.status = status
alt.idc = idc
alt.purchase_day = purchase_day
alt.save()
message = '修改成功'
return redirect('/assets/index/')
else:
message = '修改失败'
edit_form = forms.EditForm()
print('1')
return render(request, 'assets/edit.html',{'Edit_FormInput':edit_form})
else:
asset_type = models.Asset.objects.only('asset_type').get(id=asset_id).asset_type
name = models.Asset.objects.only('name').get(id=asset_id).name
sn = models.Asset.objects.only('sn').get(id=asset_id).sn
manufacturer = models.Asset.objects.only('manufacturer').get(id=asset_id).manufacturer
status = models.Asset.objects.only('status').get(id=asset_id).status
idc = models.Asset.objects.only('idc').get(id=asset_id).idc
purchase_day = models.Asset.objects.only('purchase_day').get(id=asset_id).purchase_day
form = forms.EditForm(
initial={
'asset_type':asset_type,
'name': name,
'sn': sn,
'manufacturer': manufacturer,
'status': status,
'idc': idc,
'm_time': purchase_day,
}
)
return render(request, 'assets/edit.html', {'Edit_FormInput':form})
(3)del_equipment
该功能不需要具体页面,只需要将asset_id传到函数del_equipment()中去,在主页面添加确定窗口。
def del_equipment(request,asset_id):
models.Asset.objects.get(id=asset_id).delete()
return redirect('/assets/index/')
del.PNG
3.3index页面按钮
datatable提供了button功能,Datatables 表格插件文章有具体介绍,具体可参考官网文档。主要添加必要js:
<!-- DataTable buttons -->
<script src="/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
<script src="/datatables.net-buttons-bs/js/buttons.bootstrap.min.js"></script>
<!-- buttons 打印功能 -->
<script src="/datatables.net-buttons/js/buttons.print.min.js"></script>
<!-- buttons 导出功能 -->
<script src="/datatables.net-buttons/js/buttons.html5.min.js"></script>
<script src="/jszip/dist/jszip.min.js"></script>
在$('#assets_table').DataTable中添加设置:
dom: '<"pull-left"B>ft<"pull-left"i>p',
buttons: [
{
text: '<i class="glyphicon glyphicon-copy" title="复制"></i>',
extend: 'copy'
}, {
text: '<i class="glyphicon glyphicon-print" title="打印"></i>',
extend: 'print'
}, {
text: '<i class="glyphicon glyphicon-list-alt" title="导出csv"></i>',
extend: 'csv'
},
],
由于datatable默认英文,因此在设置中添加language选项:
language:{
'sSearch':"搜索",
"info": "显示 _START_ 到 _END_ 页共 _TOTAL_ 条",
"infoEmpty": "显示 0 到 0 页共 0 条",
"lengthMenu": "显示 _MENU_ 条",
"zeroRecords": "没有匹配项",
"paginate": {
"first": "首页",
"last": "末页",
"next": "下页",
"previous": "上页"
},
具体效果为:
button.PNG
print.PNG
4.下步工作
(1)目前数据库还是使用Django内置的sqlite,下步将使用mysql实现;
(2)网站的部署;
网友评论