导入StringField和URLField的问题

时间:2018-11-01 16:48:17

标签: flask flask-wtforms wtforms

from flask_wtf import FlaskForm
from flask_wtf.html5 import URLField

我收到此错误:

  

“ FlaskWTFDeprecationWarning:flask_wtf.html5”将从1.0中删除。直接从“ wtforms.fields.html5”和“ wtforms.widgets.html5”导入。

所以我尝试了

from wtforms.fields.html5 import StringField
from wtforms.widgets.html5 import URLField

但根本不起作用..

可以有人帮助..提前谢谢

2 个答案:

答案 0 :(得分:0)

在您的代码中尝试类似的操作

from wtforms.widgets.html5 import URLInput, Input

OR

from flask_wtf import FlaskForm as Form
from wtforms import (StringField, SubmitField)
from wtforms.validators import (DataRequired, Regexp)

# And then create your form with something like the following code

class WhateverForm(Form):
    name = StringField('Name*', validators=[
        DataRequired('Name is required'),])
    url = StringField('URL*', validators=[
        DataRequired('URL is required'),
        Regexp('^(http|https):\/\/[\w.\-]+(\.[\w.\-]+)+.*$', 0,
               'URL must be a valid link')])
    submit = SubmitField()

答案 1 :(得分:0)

HTML5 小部件和字段自 1.0.4 起内置于 wtforms。您应该考虑从 wtforms 导入它们。为此,请使用 from wtforms import URLField