本文整理匯總了Python中django.forms.GenericIPAddressField方法的典型用法代碼示例。如果您正苦於以下問題:Python forms.GenericIPAddressField方法的具體用法?Python forms.GenericIPAddressField怎麽用?Python forms.GenericIPAddressField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.forms
的用法示例。
在下文中一共展示了forms.GenericIPAddressField方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_generic_ipaddress_as_ipv4_only
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def test_generic_ipaddress_as_ipv4_only(self):
f = GenericIPAddressField(protocol="IPv4")
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
f.clean('')
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
f.clean(None)
self.assertEqual(f.clean(' 127.0.0.1 '), '127.0.0.1')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"):
f.clean('foo')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"):
f.clean('127.0.0.')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"):
f.clean('1.2.3.4.5')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"):
f.clean('256.125.1.5')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"):
f.clean('fe80::223:6cff:fe8a:2e8a')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"):
f.clean('2a02::223:6cff:fe8a:2e8a')
示例2: test_generic_ipaddress_as_generic_not_required
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def test_generic_ipaddress_as_generic_not_required(self):
f = GenericIPAddressField(required=False)
self.assertEqual(f.clean(''), '')
self.assertEqual(f.clean(None), '')
self.assertEqual(f.clean('127.0.0.1'), '127.0.0.1')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 or IPv6 address.'"):
f.clean('foo')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 or IPv6 address.'"):
f.clean('127.0.0.')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 or IPv6 address.'"):
f.clean('1.2.3.4.5')
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 or IPv6 address.'"):
f.clean('256.125.1.5')
self.assertEqual(f.clean(' fe80::223:6cff:fe8a:2e8a '), 'fe80::223:6cff:fe8a:2e8a')
self.assertEqual(f.clean(' 2a02::223:6cff:fe8a:2e8a '), '2a02::223:6cff:fe8a:2e8a')
with self.assertRaisesMessage(ValidationError, "'This is not a valid IPv6 address.'"):
f.clean('12345:2:3:4')
with self.assertRaisesMessage(ValidationError, "'This is not a valid IPv6 address.'"):
f.clean('1::2:3::4')
with self.assertRaisesMessage(ValidationError, "'This is not a valid IPv6 address.'"):
f.clean('foo::223:6cff:fe8a:2e8a')
with self.assertRaisesMessage(ValidationError, "'This is not a valid IPv6 address.'"):
f.clean('1::2:3:4:5:6:7:8')
with self.assertRaisesMessage(ValidationError, "'This is not a valid IPv6 address.'"):
f.clean('1:2')
示例3: __init__
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def __init__(self, verbose_name=None, name=None, protocol='both',
unpack_ipv4=False, *args, **kwargs):
self.unpack_ipv4 = unpack_ipv4
self.protocol = protocol
self.default_validators, invalid_error_message = \
validators.ip_address_validators(protocol, unpack_ipv4)
self.default_error_messages['invalid'] = invalid_error_message
kwargs['max_length'] = 39
super(GenericIPAddressField, self).__init__(verbose_name, name, *args,
**kwargs)
示例4: check
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def check(self, **kwargs):
errors = super(GenericIPAddressField, self).check(**kwargs)
errors.extend(self._check_blank_and_null_values(**kwargs))
return errors
示例5: deconstruct
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def deconstruct(self):
name, path, args, kwargs = super(GenericIPAddressField, self).deconstruct()
if self.unpack_ipv4 is not False:
kwargs['unpack_ipv4'] = self.unpack_ipv4
if self.protocol != "both":
kwargs['protocol'] = self.protocol
if kwargs.get("max_length", None) == 39:
del kwargs['max_length']
return name, path, args, kwargs
示例6: get_internal_type
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def get_internal_type(self):
return "GenericIPAddressField"
示例7: get_prep_value
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def get_prep_value(self, value):
value = super(GenericIPAddressField, self).get_prep_value(value)
if value is None:
return None
if value and ':' in value:
try:
return clean_ipv6_address(value, self.unpack_ipv4)
except exceptions.ValidationError:
pass
return six.text_type(value)
示例8: formfield
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def formfield(self, **kwargs):
defaults = {
'protocol': self.protocol,
'form_class': forms.GenericIPAddressField,
}
defaults.update(kwargs)
return super().formfield(**defaults)
示例9: formfield
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def formfield(self, **kwargs):
return super().formfield(**{
'protocol': self.protocol,
'form_class': forms.GenericIPAddressField,
**kwargs,
})
示例10: create_ipaddress_field
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def create_ipaddress_field(self, field, options):
return forms.GenericIPAddressField(**options)
示例11: test_adding_custom_field
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def test_adding_custom_field(self):
"""Tests that we can add the ipaddress field, which is an extended choice."""
ExtendedFormField.objects.create(
page=self.form_page,
sort_order=1,
label='Device IP Address',
field_type='ipaddress',
required=True,
)
form_class = self.form_page.get_form_class()
form = form_class()
# check ip address field used
self.assertIsInstance(
form.base_fields['device-ip-address'], forms.GenericIPAddressField)
示例12: deconstruct
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def deconstruct(self):
name, path, args, kwargs = super(GenericIPAddressField, self).deconstruct()
if self.unpack_ipv4 is not False:
kwargs['unpack_ipv4'] = self.unpack_ipv4
if self.protocol != "both":
kwargs['protocol'] = self.protocol
if kwargs.get("max_length") == 39:
del kwargs['max_length']
return name, path, args, kwargs
示例13: formfield
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import GenericIPAddressField [as 別名]
def formfield(self, **kwargs):
defaults = {'form_class': forms.GenericIPAddressField}
defaults.update(kwargs)
return super(GenericIPAddressField, self).formfield(**defaults)