我有这样的表格:
class NetworkInput(forms.Form):
IP = forms.GenericIPAddressField()
Netmask = forms.IntegerField()
用户应该能够输入IPv4或IPv6地址。 根据IP版本,Netmask的验证应如下所示:
import ipcalc
IP = ipcalc.IP(IP)
if IP.version() == 4:
if Netmask > 29:
raise ValidationError(u'%s is not big enough' % value)
else:
if Netmask > 125:
raise ValidationError(u'%s is not big enough' % value)
但在验证网络掩码时,我不知道如何访问变量IP。
答案 0 :(得分:1)
创建一个执行组合验证的clean()方法,例如
def clean(self):
IP = self.cleaned_data['IP']
Netmask = self.cleaned_data['Netmask']
IP = ipcalc.IP(IP)
if IP.version() == 4:
if Netmask > 29:
raise ValidationError(u'%s is not big enough' % value)
else:
if Netmask > 125:
raise ValidationError(u'%s is not big enough' % value)