本文整理汇总了Python中wtforms.validators.URL属性的典型用法代码示例。如果您正苦于以下问题:Python validators.URL属性的具体用法?Python validators.URL怎么用?Python validators.URL使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类wtforms.validators
的用法示例。
在下文中一共展示了validators.URL属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import URL [as 别名]
def validate(self):
if self.contact_details_type.data == 'url':
self.url.validators = [DataRequired(), URL(message='Must be a valid URL')]
elif self.contact_details_type.data == 'email_address':
self.email_address.validators = [DataRequired(), Length(min=5, max=255), ValidEmail()]
elif self.contact_details_type.data == 'phone_number':
# we can't use the existing phone number validation functions here since we want to allow landlines
def valid_phone_number(self, num):
try:
normalise_phone_number(num.data)
return True
except InvalidPhoneError:
raise ValidationError('Must be a valid phone number')
self.phone_number.validators = [DataRequired(), Length(min=5, max=20), valid_phone_number]
return super().validate()
示例2: __init__
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import URL [as 别名]
def __init__(self):
self.message = ERROR_INVALID_URL
self.regex = URLValidator().regex
示例3: validate_image
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import URL [as 别名]
def validate_image(form, field):
if field.data == field.default:
return True
url_validator = validators.URL()
return url_validator(form, field)
示例4: get_field_args
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import URL [as 别名]
def get_field_args(field, **kwargs):
"""
Generates a dictionary of arguments to be used when
rendering out a form field.
Args:
field: The form field to render.
**kwargs: Any additional arguments to include for the form field.
Returns:
A dictionary of arguments to use to render out a form field.
"""
# Set up our default args
field_args = {
"class_": "form-control"
}
# Handle required fields
if field.flags.required:
field_args['required'] = 'required'
# Look at field validators
for val in field.validators:
# Handle minlength/maxlength attributes if specified on
# string fields through a Length validator
if isinstance(val, Length):
if val.min > 0:
field_args['minlength'] = val.min
if val.max > 0:
field_args['maxlength'] = val.max
elif isinstance(val, Email):
field_args['type'] = 'email'
elif isinstance(val, URL):
field_args['type'] = 'url'
elif isinstance(val, NumberRange):
if val.min is not None:
field_args['min'] = val.min
if val.max is not None:
field_args['max'] = val.max
# If we have a description, create an aria-described by attribute
if field.description and len(field.description) > 0:
field_args['aria-describedby'] = field.id + '_help'
# Merge in extra arguments
field_args.update(kwargs)
# Default rows for textareas if not specified
if 'rows' not in field_args and field.type == 'TextAreaField':
field_args['rows'] = '3'
return field_args