2017-05-10 86 views
1

我遇到了表單驗證的麻煩。國家名單正確生成,以前的表格工作正常。它僅在POST請求中打破。燒瓶平臺TypeError:__init __()需要1到2個位置參數,但有3個被給出

這裏是我的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

在settings.html中我使用{{form.field}}來生成每個表單域 – jmrueda

+0

嘗試'form = ProfileForm(request.POST)'而不是'ProfileForm(request.form)'。如果你提供了其他堆棧跟蹤 –

+0

我不起作用會有所幫助。這裏是堆棧跟蹤:https://pastebin.com/Lq00syTz – jmrueda

回答

4

應該validators.InputRequired()而不是validators.InputRequired。謝謝@jackevans

相關問題