flask wtform TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
Asked Answered
G

1

7

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 )
Galingale answered 10/5, 2017 at 11:56 Comment(7)
in settings.html i am using {{ form.field}} to generate each form fieldGalingale
Try form = ProfileForm(request.POST) instead of ProfileForm(request.form). Would help if you provided the rest of the stack traceBoony
I doesnt work. here is the stack trace: pastebin.com/Lq00syTzGalingale
If I print form.data everything seems fine: {'news': True, 'username': 'asdf', 'country': 'Aruba', 'company': 'asDf', 'email': '[email protected]'}Galingale
Check the form.errors dictionary to see if it contains anything. It could potentially be missing a CSRF keyBoony
No form errors detected. I am using directly wtforms, not flask-wtforms, I read the docs and think i do not need a CSRF key ?Galingale
if i do not validate (if request.method == 'POST':) it works, so it is the form.validate()Galingale
G
28

It should be validators.InputRequired() instead of validators.InputRequired. Thanks @jackevans

Galingale answered 10/5, 2017 at 13:16 Comment(1)
So much time waisted... it's a pitty Python does not catch such stupid mistakes earlier. Sometimes I am wondering how is it possible to build anything robust in dynamically-typed languages.. Thank you @jmrueda.Feller

© 2022 - 2024 — McMap. All rights reserved.