python - flask wtform TypeError : __init__() takes from 1 to 2 positional arguments but 3 were given

标签 python flask wtforms

我在表单验证方面遇到了问题。国家列表生成正确,以前的表格工作正常。它只会中断 POST 请求。

这是我的forms.py:

from wtforms import Form, BooleanField, SelectField, \
                    StringField, PasswordField, SubmitField, validators, \
                    RadioField
from ..models import User
from pycountry import countries
...
## Account settings
# We get all COUNTRIES
COUNTRIES = [(c.name, c.name) for c in countries]
# edit profile
class ProfileForm(Form):
    username = StringField('name',[validators.Length(min=1, max=120), validators.InputRequired])
    email = StringField('email', [validators.Length(min=6, max=120), validators.Email()])
    company = StringField('name',[validators.Length(min=1, max=120)])
    country = SelectField('country', choices=COUNTRIES)
    news = BooleanField('news')

这是 View :

@user.route('/profile/', methods=['GET', 'POST'])
@login_required
def profile():
    userid = current_user.get_id()
    user = User.query.filter_by(id=userid).first_or_404()
    print(user)
    form = ProfileForm(request.form)
    if request.method == 'POST' and form.validate():
        user.username = form.username.data
        ...
        return render_template('settings.html', form=form )
    else:
        form.username.data = user.username
        ...
        return render_template('settings.html', form=form )

最佳答案

应该是validators.InputRequired()而不是validators.InputRequired。谢谢@jackevans

关于python - flask wtform TypeError : __init__() takes from 1 to 2 positional arguments but 3 were given,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43891822/

相关文章:

python - 如何使用sql读取csv

python - 将参数传递给回调函数

python - 在Python中,将期望值与实际值相匹配的好方法是什么?

python - 如何从数据库中检索列名

python - nginx - 从上游读取响应 header 时上游发送了太大的 header

python - wtforms 验证下拉值

python - 在 WTForms 中使用 HTML5 字段

python - 使用 Python 计算内容长度

python - flask form.validate_on_submit() 使用 wtforms 失败

python - Wtforms:添加具有多重继承的动态字段