cmdb之增删改查续

前面完成了展示页面的功能,继续后面的功能

  • 整个项目的路由
    urls.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    from 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/(\d+)/', views.Update.as_view()),
    url(r'del', views.Del.as_view()),
    ]

edit后面使用正则表达式,作用在后面的 views.py 可以看到

  • 页面基于 form 类实现
    /utils/for_class.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
    43
    44
    45
    46
    47
    48
    49
    50
    51
    from django.forms import fields, widgets, Form
    from demo01 import models


    class HostForm(Form):
    hostname = fields.CharField(
    required=True,
    widget=widgets.TextInput(attrs={'class': 'form-control'})
    )

    ecsname = fields.CharField(
    required=True,
    widget=widgets.TextInput(attrs={'class': 'form-control'})
    )

    cpu = fields.IntegerField(
    required=True,
    widget=widgets.TextInput(attrs={'class': 'form-control'})
    )

    mem = fields.IntegerField(
    required=True,
    widget=widgets.TextInput(attrs={'class': 'form-control'})
    )

    speed = fields.CharField(
    required=True,
    widget=widgets.TextInput(attrs={'class': 'form-control'})
    )

    network = fields.CharField(
    required=True,
    widget=widgets.TextInput(attrs={'class': 'form-control'})
    )

    source_id = fields.ChoiceField(
    required=True,
    choices=[],
    widget=widgets.Select(attrs={'class': 'form-control'})
    )

    region_id = fields.ChoiceField(
    required=True,
    choices=[],
    widget=widgets.Select(attrs={'class': 'form-control'})
    )

    def __init__(self, *args, **kwargs):
    super(HostForm, self).__init__(*args, **kwargs)
    self.fields['source_id'].choices = models.Source.objects.values_list('id', 'name')
    self.fields['region_id'].choices = models.Region.objects.values_list('id', 'name')
  • 注意点1
    表的一对多关系中,对应的数据不能直接在 form 类中写死,应该在 init 方法中实现,如上面的 source/region
    form 类中写死,如果数据有更新,必须重启服务才能取到更新后的值
    init 方法中,每次实例化 form 类都会重新取值

  • 注意点2
    对于 source/region 一对多关系中,插入时需要注意值的类型
    如上面的 source 字段,默认取到的为字符串,需要使用 名称_id 方式才能插入整数类型

增加

/host/views.py

1
2
3
4
5
6
7
8
9
10
11
12
class Add(View):
def post(self, request, *args, **kwargs):
form = form_class.HostForm(data=request.POST)
if form.is_valid():
models.Host.objects.create(**form.cleaned_data)
return redirect('/host/list')
else:
return render(request, 'add.html', locals())

def get(self, request, *args, **kwargs):
form = form_class.HostForm()
return render(request, 'add.html', locals())

/templates/add.html

1
2
3
4
5
6
7
8
9
10
11
12
13
<form method="post" novalidate>
{% csrf_token %}
<p id="hostname">阿里云主机名:{{ form.hostname }}{{ form.errors.hostname.0 }}</p>
<p id="ecsname">阿里云实例名:{{ form.ecsname }}{{ form.errors.ecsname.0 }}</p>
<p id="cpu">CPU::{{ form.cpu }}{{ form.errors.cpu.0 }}</p>
<p id="mem">内存/G:{{ form.mem }}{{ form.errors.mem.0 }}</p>
<p id="speed">带宽/M:{{ form.speed }}{{ form.errors.speed.0 }}</p>
<p id="nework">IP:{{ form.nework }}{{ form.errors.nework.0 }}</p>
<p id="source">来源类型:{{ form.source_id }}{{ form.errors.source_id.0 }}</p>
<p id="region">所属区域:{{ form.region_id }}{{ form.errors.region_id.0 }}</p>

<input type="submit" value="提交">
</form>

删除

/host/views.py

1
2
3
4
5
6
7
8
9
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')

更新

/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
class Update(View):
def post(self, request, pk):
form = form_class.HostForm(data=request.POST)
if form.is_valid():
models.Host.objects.filter(id=pk).update(**form.cleaned_data)
return redirect('/host/list')
else:
return render(request, 'edit.html', locals())

def get(self, request, pk):
obj = models.Host.objects.filter(id=pk).first()
form = form_class.HostForm(
initial={
'hostname': obj.hostname,
'ecsname': obj.ecsname,
'cpu': obj.cpu,
'mem': obj.mem,
'speed': obj.speed,
'source_id': obj.source_id,
'region_id': obj.region_id,
}
)
return render(request, 'edit.html', locals())

/templates/edit.html

1
2
3
4
5
6
7
8
9
10
11
12
13
<form method="post" novalidate>
{% csrf_token %}
<p id="hostname">阿里云主机名:{{ form.hostname }}{{ form.errors.hostname.0 }}</p>
<p id="ecsname">阿里云实例名:{{ form.ecsname }}{{ form.errors.ecsname.0 }}</p>
<p id="cpu">CPU::{{ form.cpu }}{{ form.errors.cpu.0 }}</p>
<p id="mem">内存/G:{{ form.mem }}{{ form.errors.mem.0 }}</p>
<p id="speed">带宽/M:{{ form.speed }}{{ form.errors.speed.0 }}</p>
<p id="nework">IP:{{ form.nework }}{{ form.errors.nework.0 }}</p>
<p id="source">来源类型:{{ form.source_id }}{{ form.errors.source_id.0 }}</p>
<p id="region">所属区域:{{ form.region_id }}{{ form.errors.region_id.0 }}</p>

<input type="submit" value="提交">
</form>

Recommended Posts