0

I have this form:

class NetworkInput(forms.Form):          
    IP = forms.GenericIPAddressField()     
    Netmask = forms.IntegerField()

The users should be able to enter an IPv4 or an IPv6 address. Depending of the IP version the validation of Netmask should look like this:

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)

But I don't know how to access the variable IP when validating the Netmask.

4

1 に答える 1

1

https://docs.djangoproject.com/en/1.5/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-otherのdjangoドキュメントで説明されているように

結合された検証を行う 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)
于 2013-10-16T10:26:29.430 に答える