Flask:form.validate_on_submit()抛出类型错误

时间:2017-06-29 12:53:06

标签: flask flask-wtforms

每次我在'/ signup'视图中提交表单时,我的form.validate_on_submit()中的views.py都会引发以下错误:

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

堆栈跟踪很长,我没有看到任何明显的东西。我不知道为什么会这样做。我按照Flask-WTF docs进行了验证表格。

编辑:Here是我看到的堆栈跟踪。

views.py

from myapp import app
from flask import render_template, redirect
from forms import RegistrationForm

@app.route('/', methods=['POST', 'GET'])
@app.route('/signup', methods=['POST', 'GET'])
def signup():
    form = RegistrationForm()
    if form.validate_on_submit():
        # Redirect to Dash Board
        return redirect('/dashboard')
    return render_template("signup.html", form=form)

@app.route('/login')
def login():
    return "<h1>Login</h1>"

@app.route('/dashboard')
def dashboard():
    return "<h1>Dashboard</h1>"

forms.py

from flask_wtf import FlaskForm
from wtforms import TextField, PasswordField
from wtforms.validators import InputRequired, Email, Length

class RegistrationForm(FlaskForm):
    username = TextField('username', validators=[InputRequired(), Length(min=4, max=30)])
    email = TextField('email', validators=[InputRequired(), Email, Length(max=25)])
    password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])

class LoginForm(FlaskForm):
    username = TextField('username', validators=[InputRequired(), Length(min=4, max=30)])
    password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])

signup.html

{% extends "base.html" %}
{% block content %}
<h1>Sign Up</h1>

<form method="POST" action="/signup">
{{ form.hidden_tag() }}
<p>Username:</p>
{{ form.username() }}
<p>Email:</p>
{{ form.email() }}
<p>Password:</p>
{{ form.password() }}
<br/>
<br/>
<button type="Submit" value="submit" name="submit">Submit</button>
</form>
{% endblock %}

1 个答案:

答案 0 :(得分:3)

我明白了!在forms.py中,我的RegistrationForm email属性应为:

email = TextField('email', validators=[InputRequired(), Email(), Length(max=25)])

我忘记了Email参数的错误括号。