新建 django 项目,然后新建 host 应用,cmdb 主机管理部分的内容在应用 host 中完成。
项目级别 urls.py
1
2
3
4
5
6
7from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^host/', include('host.urls')),
]
此处使用 include 表示 url 中 host 之后部分的内容将到 host 应用的 urls 中匹配/host/urls.py
1
2
3
4
5
6
7
8
9from django.conf.urls import url,include
from host import views
urlpatterns = [
url(r'list$', views.List.as_view()),
url(r'add$', views.Add.as_view()),
url(r'edit', views.Update.as_view()),
url(r'del', views.Del.as_view()),
]
此处采用 CBV 写法,注意 list 和 add 后面都有结束符 $ ,但是 edit 和 del 后面没有
因为查询和增加不需要已有的数据,更新和删除都是针对已有的数据, url 中需要传递参数modles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class Host(models.Model):
hostname= models.CharField(max_length=64, blank=True, null=True, verbose_name='阿里云主机名')
ecsname= models.CharField(max_length=64, blank=True, null=True, verbose_name='阿里云实例ID')
cpu= models.CharField(max_length=8,blank=True, null=True, verbose_name='CPU')
lab= models.ForeignKey(to='Lable',blank=True, null=True, verbose_name='标签')
mem= models.CharField(max_length=8,blank=True, null=True, verbose_name='内存/M')
speed = models.CharField(max_length=8,blank=True, default='5',null=True, verbose_name='带宽/M')
disks= models.ManyToManyField(to='Disk', blank=True, null=True, verbose_name='磁盘')
state_choices=(
(1,'Running'),
(2,'下线'),
(3,'关机'),
(4,'删除'),
)
state = models.SmallIntegerField(verbose_name='主机状态', choices=state_choices,blank=True,null=True,)
def __str__(self):
return self.eth0_network
class Meta:
verbose_name_plural = "主机表"
/host/views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43from django.shortcuts import render,redirect,HttpResponse
from django.views import View
from demo01 import models
# Create your views here.
class List(View):
def post(self, request, *args, **kwargs):
pass
def get(self, request, *args, **kwargs):
host_list=models.Host.objects.all()
return render(request, 'host.html', locals())
class Add(View):
def post(self, request, *args, **kwargs):
pass
def get(self, request, *args, **kwargs):
return HttpResponse('add')
class Update(View):
def post(self, request, *args, **kwargs):
pass
def get(self, request, *args, **kwargs):
get_id = int(request.Get.get('id'))
obj = models.Host.objects.filter(id=get_id).first()
return redirect('/host/list')
class Del(View):
def post(self, request, *args, **kwargs):
pass
def get(self, request, *args, **kwargs):
return HttpResponse('del')
get_id = int(request.Get.get('id'))
models.Host.objects.filter(id=get_id).delete()
return redirect('/host/list')
展示页面/template/host.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主机列表</title>
</head>
<body>
<div>
<a href="/host/add">添加</a>
</div>
<div class="container">
<table class="table table-bordered table-hover" id="table">
<thead>
<tr>
<th></th>
<th>主机名</th>
<th>CPU</th>
<th>内存/G</th>
<th>带宽/M</th>
<th>IP</th>
<th>来源类型</th>
<th>所属区域</th>
<th>状态</th>
<th>编辑</th>
<th>删除</th>
</tr>
</thead>
<tbody>
{% for host in host_list %}
<tr>
<td>{{ host.id }}</td>
<td>{{ host.hostname }}</td>
<td>{{ host.cpu }}</td>
<td>{{ host.mem }}</td>
<td>{{ host.speed }}</td>
<td>{{ host.network }}</td>
<td>{{ host.source }}</td>
<td>{{ host.region }}</td>
<td>{{ host.get_state_display }}</td>
<td><a href="/host/edit/id={{ host.id }}">编辑</a></td>
<td><a href="/host/del?id={{ host.id }}">编辑</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
- 注意事项
要是使用 mysql 数据库,除了修改 settings.py 文件,还需要在项目名文件的 init ,在里面写入
1
2import pymysql
pymysql.install_as_MySQLdb()通过 url 传递的参数
/host/edit?id=1
得到的是字符串而不是数字,注意类型转换- models.Host.objects.filter(id=get_id).first() 得到的是 queryset
models.Host.objects.filter(id=get_id) 得到的是 queryset list models.py
中定义 class 时候建议添加__str__
方法,返回友好的数据- choices 使用方法在 models.py 中可以看到,取值的时候通过
get_名称_display
实现。