本文整理汇总了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)