每次我在'/ signup'视图中提交表单时,我的form.validate_on_submit()
中的views.py
都会引发以下错误:
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
堆栈跟踪很长,我没有看到任何明显的东西。我不知道为什么会这样做。我按照Flask-WTF docs进行了验证表格。
编辑:Here是我看到的堆栈跟踪。
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>"
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)])
{% 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 %}
答案 0 :(得分:3)
我明白了!在forms.py
中,我的RegistrationForm
email
属性应为:
email = TextField('email', validators=[InputRequired(), Email(), Length(max=25)])
我忘记了Email
参数的错误括号。