每当我尝试使用 flask-wtf 时出现 HostnameValidation 错误

HostnameValidation error whenever i try to use with flask-wtf

我之前在 google 搜索过但没有结果,卡住了 3 天后我决定寻求帮助

Information:
WTForms==2.2.1
Flask==1.0.2
Flask-WTF==0.14.2 

还有人使用过 HostnameValidation。我正在对表单中的输入进行验证,但似乎每当我尝试使用此验证方法 with/without 参数 allow_ip= True 时。它给了我一个错误

class ServiceForm(FlaskForm):
    name = StringField('Service Name',[validators.DataRequired(), validators.Length(min=2), validators.Regexp("^[A-Za-z0-9_-]*$",message='Service Name must be number and letter only. No space!')])
    type = SelectField('Service Type', choices=[('2','Check Port')])
    host = StringField('IP Host', [ validators.HostnameValidation( allow_ip= True)])
    port = DecimalField('Port Number',[validators.DataRequired(), validators.NumberRange(min=10,max=65535,message='Port must be between 10-65535')])

注意:如果我将 HostnameValidation 更改为另一种方法,它可以正常工作,但它无法提供我想要执行的足够验证。我想验证该字段是否是有效的 IP 地址或主机名

HostnameValidation 没有出现在文档中是有原因的。据我所知,它仅用于 URL Validator for the host part of the URL. Its own docstring 提供必要的解释:

Helper class for checking hostnames for validation.

This is not a validator in and of itself, and as such is not exported.

您可以根据您的具体情况获取 class 和 write a custom WTForms validator 的代码。