TypeError:__ init __()取1到2个位置参数,但给出了3个

时间:2017-09-03 08:41:06

标签: python flask flask-wtforms

我正在学习Flask,但我不能让我的表格正常运作。表单应该获取用户的名称并将其打印在同一页面上

形式:

class LoginForm(FlaskForm):
    name = StringField(
        'What is your name?',
        validators=[Length(min = 4, max = 25), Required])
    submit = SubmitField('Submit')

查看:

@app.route('/login/', methods=['GET','POST'])
def login():
    name = None
    formi = LoginForm()
    if formi.validate_on_submit():
        name = formi.name.data
        formi.name.data = ''
    return render_template('LoginForm.html', formi = formi, name = name)

模板:

<form action="" method="post" name="login">
{{ formi.hidden_tag() }}
<p>Enter your nickname</p>
{{ formi.name(size=25) }}
<input type="submit" value="Submit form">
</form>

错误:

TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given

1 个答案:

答案 0 :(得分:0)

请检查拼写是否正确>“ formi”

@app.route('/login/', methods=['GET','POST'])
def login():
    name = None
    **formi** = LoginForm()
    if formi.validate_on_submit():
        name = formi.name.data
        formi.name.data = ''
    return render_template('LoginForm.html', formi = formi, name = name)