我在表单验证方面遇到麻烦。国家列表正确生成,并且以前的表格工作正常。它只是在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/

10-10 22:01