2019-1-15 10:46:51
管理系统list
- 采用CBV方式,代码少又美观
- 数据库modles.py比较简单(未涉及多对多,一对多等复杂方式,还不太了解怎么使用)
- 使用form表单来生成前端数据
# modles.py
from django.db import models
# Create your models here.
class HostList(models.Model):
hostname = models.CharField(max_length=32,null=True,verbose_name='主机名')
ipaddress = models.CharField(max_length=32,null=True,verbose_name='IP地址')
cpu = models.CharField(max_length=32,null=True,verbose_name='CPU')
mem = models.CharField(max_length=32,null=True,verbose_name='内存')
disk = models.CharField(max_length=32,null=True,verbose_name='磁盘')
region = models.CharField(max_length=32,null=True,verbose_name='机房')
# region = models.ForeignKey(to='Region',null=True,verbose_name='机房')
system = models.CharField(max_length=32,null=True,verbose_name='操作系统')
# class Region(models.Model):
# name = models.CharField(max_length=32,null=True,verbose_name='机房')
# def __str__(self):
# return self.name
# class Meta:
# verbose_name_plural = '机房表'
class UserInfo(models.Model):
username = models.CharField(max_length=32,null=True)
password = models.CharField(max_length=32,null=True)
# urls.py
from django.conf.urls import url,include
from django.contrib import admin
from host import views
urlpatterns = [
url(r'^hostpage/',views.hostpage.as_view()),
url(r'^add/',views.add.as_view()),
url(r'^update/(/d+)/',views.update.as_view()),
url(r'delete',views.delete.as_view()),
# url(r'edit/',views.edit.as_view()),
url(r'^search/',views.search.as_view()),
# url(r'',views.login),
url(r'',views.login),
url(r'^logout/',views.logout),
url(r'^register/',views.register),
对数据进行增删改查(基于form)
class hostpage(View):
def get(self,request,*args,**kwargs):
obj_li = models.HostList.objects.all()
return render(request, 'hostpage.html', locals())
def post(self):
pass
# 增
class add(View):
def get(self,request):
form =form_class.HostForm()
return render(request,'add.html',locals())
def post(self,request):
form = form_class.HostForm(data=request.POST)
print('add_post')
try:
if form.is_valid():
print(form.cleaned_data)
print(type(form.cleaned_data['region']))
models.HostList.objects.create(**form.cleaned_data)
print('ok')
# ong.update(**form.cleaned_data)
return redirect('/hostpage/')
except AttributeError:
return render(request, 'add.html', locals())
return redirect('/hostpage/')
# 删
class delete(View):
def post(self,request,*args):
print(222222222,args)
def get(self,request,*args,**kwargs):
get_id = int(request.GET.get('id'))
models.HostList.objects.filter(id=get_id).delete()
return redirect('/hostpage/')
#查
# def search(request):
# if request.POST:
# hostname = request.POST['hostname']
# obj1 = models.HostList.objects.filter(hostname=hostname)
# return render(request, 'hostpage.html', {'obj1': obj1})
#改
class update(View):
def get(self,request,getid):
getid = getid
obj = models.HostList.objects.filter(id=getid).first()
form = form_class.HostForm(
initial={
'hostname':obj.hostname,
'ipaddress':obj.ipaddress,
'cpu':obj.cpu,
'mem':obj.mem,
'disk':obj.disk,
'region':obj.region,
'system':obj.system,
}
)
return render(request,'update.html',locals())
def post(self,request,getpid):
try:
form = form_class.HostForm(data=request.POST)
print('post',getpid)
if form.is_valid():
print(form.cleaned_data)
ong = models.HostList.objects.filter(id=getpid).update(**form.cleaned_data)
# ong.update(**form.cleaned_data)
return redirect('/hostpage/')
except AttributeError:
return render(request,'update.html',locals())
return redirect('/hostpage/')
# hostpage.html
<table class="table table-hover">
<thead>
<h1 align="center">服务器列表</h1>
{# <h4 align="left">登录用户:{{ v }}</h4>#}
<div>
<a href="/add/"><button>新增</button></a>
</div>
<tr>
<th>序号</th>
<th>主机名</th>
<th>IP地址</th>
<th>操作系统</th>
<th>CPU</th>
<th>磁盘</th>
<th>内存</th>
<th>机房</th>
</tr>
</thead>
<tbody>
{% for i in obj_li %}
<tr>
<th scope="row">{{ i.id }}</th>
<td>{{ i.hostname }}</td>
<td>{{ i.ipaddress }}</td>
<td>{{ i.cpu }}</td>
<td>{{ i.mem }}</td>
<td>{{ i.disk }}</td>
<td>{{ i.system }}</td>
<td>{{ i.region }}</td>
<td><a href="/update/{{ i.id }}/"><button>更新</button></a></td>
<td><a href="/delete?id={{ i.id }}"><button>删除</button></a></td>
</tr>
{% endfor %}
</tbody>
</table>