本文整理匯總了Python中django.forms.RegexField方法的典型用法代碼示例。如果您正苦於以下問題:Python forms.RegexField方法的具體用法?Python forms.RegexField怎麽用?Python forms.RegexField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.forms
的用法示例。
在下文中一共展示了forms.RegexField方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_regexfield_4
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_regexfield_4(self):
f = RegexField('^[0-9]+$', min_length=5, max_length=10)
with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 5 characters (it has 3).'"):
f.clean('123')
with self.assertRaisesMessage(
ValidationError,
"'Ensure this value has at least 5 characters (it has 3).', "
"'Enter a valid value.'",
):
f.clean('abc')
self.assertEqual('12345', f.clean('12345'))
self.assertEqual('1234567890', f.clean('1234567890'))
with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 10 characters (it has 11).'"):
f.clean('12345678901')
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean('12345a')
示例2: __init__
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def __init__(self, regex, required=True, help_text=None, max_length=None, min_length=None,
error_messages=None, validators=(), *args, **kwargs):
self.field = forms.RegexField(
regex=regex,
required=required,
help_text=help_text,
max_length=max_length,
min_length=min_length,
error_messages=error_messages,
validators=validators,
)
super().__init__(*args, **kwargs)
示例3: test_validators_fail
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_validators_fail(self):
field = SimpleListField(forms.RegexField("[a-e]{2}"))
with pytest.raises(exceptions.ValidationError) as excinfo:
field.clean("a,bc,de")
assert (
excinfo.value.messages[0]
== "Item 1 in the list did not validate: Enter a valid value."
)
示例4: test_should_regex_convert_string
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_should_regex_convert_string():
assert_conversion(forms.RegexField, String, "[0-9]+")
示例5: test_regexfield_1
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_regexfield_1(self):
f = RegexField('^[0-9][A-F][0-9]$')
self.assertEqual('2A2', f.clean('2A2'))
self.assertEqual('3F3', f.clean('3F3'))
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean('3G3')
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean(' 2A2')
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean('2A2 ')
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
f.clean('')
示例6: test_regexfield_2
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_regexfield_2(self):
f = RegexField('^[0-9][A-F][0-9]$', required=False)
self.assertEqual('2A2', f.clean('2A2'))
self.assertEqual('3F3', f.clean('3F3'))
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean('3G3')
self.assertEqual('', f.clean(''))
示例7: test_regexfield_3
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_regexfield_3(self):
f = RegexField(re.compile('^[0-9][A-F][0-9]$'))
self.assertEqual('2A2', f.clean('2A2'))
self.assertEqual('3F3', f.clean('3F3'))
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean('3G3')
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean(' 2A2')
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean('2A2 ')
示例8: test_regexfield_unicode_characters
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_regexfield_unicode_characters(self):
f = RegexField(r'^\w+$')
self.assertEqual('éèøçÎÎ你好', f.clean('éèøçÎÎ你好'))
示例9: test_validators_fail
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_validators_fail(self):
field = SimpleArrayField(forms.RegexField('[a-e]{2}'))
with self.assertRaises(exceptions.ValidationError) as cm:
field.clean('a,bc,de')
self.assertEqual(cm.exception.messages[0], 'Item 1 in the array did not validate: Enter a valid value.')
示例10: test_change_regex_after_init
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def test_change_regex_after_init(self):
f = RegexField('^[a-z]+$')
f.regex = '^[0-9]+$'
self.assertEqual('1234', f.clean('1234'))
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
f.clean('abcd')
示例11: __init__
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import RegexField [as 別名]
def __init__(self, regex, max_length=None, min_length=None, *args, **kwargs):
super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
self.regex = regex