Django ModelForm 小實例1
阿新 • • 發佈:2019-03-17
time head idt create print null 所在 mem 硬盤
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.測試
Django ModelForm 小實例1