我是python的初学者,django编码。我得到了一个网页,任务是允许使用表单保存数据。但我似乎无法在清理后取出数据。清洗后似乎没有任何价值。
以下是我的代码:在forms.py中
class DeviceDetailForm(ModelForm):
class Meta:
model= DeviceDetail
fields= '__all__'
def clean(self):
cleaned_data = super(DeviceDetailForm, self).clean()
hostname = cleaned_data.get('hostname')
if len(hostname) < 8:
raise forms.ValidationError('Hostname needs to be more than 8 character long, ' + hostname )
in views.py
def device_add(request):
if request.method == "POST":
device_frm = DeviceForm(request.POST)
if device_frm.is_valid():
new_device = device_frm.save()
devices = Device.objects.all()
return render(request, 'interface/device_display.html',{'devices':devices})
dd_frm = DeviceDetailForm(request.POST)
di_frm = DeviceInterfaceForm(request.POST)
if dd_frm.is_valid() and di_frm.is_valid():
if dd_frm.is_valid():
dd_frm.save()
devicedetail = dd_frm.save(commit=False)
devicedetail.save()
dd_frm.save_m2m()
deviceinterface = di_frm.save(commit=False)
deviceinterface.hostname = devicedetail.hostname
deviceinterface.save()
else:
device_frm = DeviceForm
return render(request, 'interface/device_add.html',{'form':device_frm} )
dd_frm = DeviceDetailForm()
di_frm = DeviceInterfaceForm()
context = {'dd_frm':dd_frm, 'di_frm':di_frm, 'title':'Device Add'}
return render(request, 'interface/device_add.html',context)
在models.py中
class DeviceDetail(models.Model):
hostname = models.CharField(max_length=50)
mgt_interface = models.CharField(max_length=50)
mgt_ip_addr = models.CharField(max_length=30)
subnetmask = models.CharField(max_length=30)
ssh_id = models.CharField(max_length=50)
ssh_pwd = models.CharField(max_length=50)
enable_secret = models.CharField(max_length=50)
device_model = models.CharField(max_length=50)
暂无答案!
目前还没有任何答案,快来回答吧!