我无法找出造成此错误的原因
TypeError: __init__() takes at most 2 arguments (3 given)
指向
@user_blueprint.route('/login', methods=['GET', 'POST'])
def login():
if g.user.is_authenticated:
flash("You are already Logged in", 'warning')
return redirect(url_for('members'))
error = ""
form = LoginForm()
if request.method == 'POST':
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is not None and bcrypt.check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember.data)
flash('You are sucessfuly Logged in', 'success')
return redirect(url_for('members'))
else:
error = "You have entered an incorrect username and password"
return render_template("front/login.html", forms=form, error=error)
LoginForm
看起来像这样:class LoginForm(Form):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired])
remember = BooleanField("Remember Me", default=False)
这是回溯:
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/afidegnum/PycharmProjects/gsam/project/user/views.py", line 79, in login
if form.validate_on_submit():
File "/home/afidegnum/gsam/lib/python2.7/site-packages/flask_wtf/form.py", line 166, in validate_on_submit
return self.is_submitted() and self.validate()
File "/home/afidegnum/gsam/lib/python2.7/site-packages/wtforms/form.py", line 310, in validate
return super(Form, self).validate(extra)
File "/home/afidegnum/gsam/lib/python2.7/site-packages/wtforms/form.py", line 152, in validate
if not field.validate(self, extra):
File "/home/afidegnum/gsam/lib/python2.7/site-packages/wtforms/fields/core.py", line 204, in validate
stop_validation = self._run_validation_chain(form, chain)
File "/home/afidegnum/gsam/lib/python2.7/site-packages/wtforms/fields/core.py", line 224, in _run_validation_chain
validator(form, self)
TypeError: __init__() takes at most 2 arguments (3 given)
最佳答案
您没有给您的验证者之一打电话:
password = PasswordField('Password', validators=[DataRequired])
添加
()
:password = PasswordField('Password', validators=[DataRequired()])
关于flask - TypeError:__init __()获得了意外的关键字参数'password',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34767548/