Django ModelForm 小实例1

it2022-05-07  3

1.models.py

ASSET_STATUS = ( (str(1), u"使用中"), (str(2), u"未使用"), (str(3), u"故障"), (str(4), u"其它"), ) ASSET_TYPE = ( (str(1), u"物理机"), (str(2), u"虚拟机"), (str(3), u"容器"), (str(4), u"网络设备"), (str(5), u"安全设备"), (str(6), u"其他") ) class Idc(models.Model): ids = models.CharField(u"机房标识", max_length=255, unique=True) name = models.CharField(u"机房名称", max_length=255, unique=True) address = models.CharField(u"机房地址", max_length=100, blank=True) tel = models.CharField(u"机房电话", max_length=30, blank=True) contact = models.CharField(u"客户经理", max_length=30, blank=True) contact_phone = models.CharField(u"移动电话", max_length=30, blank=True) jigui = models.CharField(u"机柜信息", max_length=30, blank=True) ip_range = models.CharField(u"IP范围", max_length=30, blank=True) bandwidth = models.CharField(u"接入带宽", max_length=30, blank=True) memo = models.TextField(u"备注信息", max_length=200, blank=True) def __str__(self): return self.name class Meta: verbose_name = u'数据中心' verbose_name_plural = verbose_name class Host(models.Model): hostname = models.CharField(max_length=50, verbose_name=u"主机名", unique=True) ip = models.GenericIPAddressField(u"管理IP", max_length=15) idc = models.ForeignKey(Idc, verbose_name=u"所在机房", on_delete=models.SET_NULL, null=True, blank=True) other_ip = models.CharField(u"其它IP", max_length=100, blank=True) asset_no = models.CharField(u"资产编号", max_length=50, blank=True) asset_type = models.CharField(u"设备类型", choices=ASSET_TYPE, max_length=30, null=True, blank=True) status = models.CharField(u"设备状态", choices=ASSET_STATUS, max_length=30, null=True, blank=True) os = models.CharField(u"操作系统", max_length=100, blank=True) vendor = models.CharField(u"设备厂商", max_length=50, blank=True) up_time = models.CharField(u"上架时间", max_length=50, blank=True) cpu_model = models.CharField(u"CPU型号", max_length=100, blank=True) cpu_num = models.CharField(u"CPU数量", max_length=100, blank=True) memory = models.CharField(u"内存大小", max_length=30, blank=True) disk = models.CharField(u"硬盘信息", max_length=255, blank=True) sn = models.CharField(u"SN号 码", max_length=60, blank=True) position = models.CharField(u"所在位置", max_length=100, blank=True) memo = models.TextField(u"备注信息", max_length=200, blank=True) def __str__(self): return self.hostname

2.forms.py

from django import forms from .models import Host from django.forms.widgets import * class HostForm(forms.ModelForm): class Meta: model = Host exclude = ("id",) widgets = { 'hostname': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;', 'placeholder': u'必填项'}), 'ip': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;', 'placeholder': u'必填项'}), 'other_ip': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'group': Select(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'asset_no': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'asset_type': Select(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'status': Select(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'os': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'vendor': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'up_time': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'cpu_model': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'cpu_num': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'memory': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'disk': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'sn': TextInput(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'idc': Select(attrs={'class': 'form-control', 'style': 'width:530px;'}), 'position': TextInput( attrs={'class': 'form-control', 'style': 'width:530px;', 'placeholder': u'物理机写位置,虚机写宿主'}), 'memo': Textarea(attrs={'rows': 4, 'cols': 15, 'class': 'form-control', 'style': 'width:530px;'}), }

3.urls.py

url(r'^host_get', views.host_get, name="host_get")

4.views.py

from t1.models import Idc, Host from t1.forms import HostForm def host_get(request): if request.method == "GET": h_form = HostForm() idc_obj = Idc.objects.all().values("name") print(idc_obj) return render(request, "t1/host_get.html", locals()) elif request.method == "POST": h_form = HostForm(request.POST) if h_form.is_valid(): print(h_form.cleaned_data) # Host.objects.create(**h_form.cleaned_data) h_form.save() return HttpResponse("数据添加成功") else: info_error = h_form.errors # <ul class="errorlist"><li>hostname<ul class="errorlist"><li>具有 主机名 的 Host 已存在。</li></ul></li></ul> print(info_error.as_json()) # msg_error = info_error.as_json() msg_error = info_error return render(request, "msg_error.html", locals())

5.html

#msg_error.html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span style="color: red;">{{ msg_error }}</span> </body> </html> #host_get.html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action=" {% url 'host_get' %}" method="POST"> {{ h_form.as_p }} <input type="submit" value="提交"> {% csrf_token %} </form> </body> </html>

5.测试

 

转载于:https://www.cnblogs.com/icemonkey/p/10544935.html

相关资源:Django中modelform组件实例用法总结

最新回复(0)