我在表单验证方面遇到了麻烦。国家/地区列表生成正确,以前的表单工作正常。它只会在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')
以下是观点:
@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 )
答案 0 :(得分:16)
它应该是validators.InputRequired()而不是validators.InputRequired。谢谢@jackevans