I am having troubles with form validation. The country list is generated correctly, and previous forms worked fine. It is only breaking in POST requests.
Here is my 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')
and here is the 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 )
form = ProfileForm(request.POST)
instead ofProfileForm(request.form)
. Would help if you provided the rest of the stack trace – Boony