得到了一个意想不到的关键字参数

得到了一个意想不到的关键字参数

本文介绍了TypeError:__init __()得到了一个意想不到的关键字参数'password'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  TypeError:__init __()至多需要2个参数3给出)

指向

<$ ($'code> @ user_blueprint.route('/ login',methods = ['GET','POST'])
def login():
如果g.user。 is_authenticated:
flash(您已经登录,警告)
返回重定向(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()
如果用户不是None并且bcrypt.check_password_hash(user.password,form.password.data):
login_user(用户,记住= form.remember.data)
flash(' sucessfuly登录','成功')
返回重定向(url_for('members'))
else
error =您输入的用户名和密码不正确
返回render_template(front / login.html,forms = form,error = error)
$ b $ LoginForm 看起来像这样:



pre $ LoginForm(Form):
username = StringField(Username,validators = [DataRequired()])
password = PasswordField('Password' ,validators = [DataRequired])
remember = BooleanField(Remember Me,default = False)

这是回溯:

 文件/home/afidegnum/gsam/lib/python2.7/site-packages /flask/app.py,第1836行,在__call__ 
中返回self.wsgi_app(environ,start_response)
文件/home/afidegnum/gsam/lib/python2.7/site-packages/flask /app.py,第1820行,在wsgi_app
response = self.make_response(self.handle_exception(e))
文件/home/afidegnum/gsam/lib/python2.7/site-packages /flask/app.py,第1403行, in handle_exception
reraise(exc_type,exc_value,tb)
文件/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py,第1817行,位于wsgi_app
response = self.full_dispatch_request()
文件/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py,第1477行,在full_dispatch_request
rv = self.handle_user_exception(e)
文件/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py,第1381行,在handle_user_exception
reraise(exc_type,
文件/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py,第1475行,在full_dispatch_request
rv = self.dispatch_request()
文件/home/afidegnum/gsam/lib/python2.7/site-packages/flask/app.py,第1461行,在dispatch_request
返回self.view_functions [rule.endpoint](* * req.view_args)
文件/home/afidegnum/PycharmProjects/gsam/project/user/views.py,第79行,在登录
如果form.validate_on_submit():
文件/家/ afidegnum / gsam / lib / python2.7 / site-packages / flask_wtf / form.py,第166行,在validate_on_submit
返回self.is_submitted()和self.validate()
文件/ home /afidegnum/gsam/lib/python2.7/site-packages/wtforms/form.py,第310行,验证
返回超级(Form,self).validate(extra)
文件/ home / afidegnum / gsam / lib / python2.7 / site-packages / wtforms / form.py,第152行,验证
(如果不是)field.validate(self,extra):
File/ home / afidegnum / gsam / lib / python2.7 / site-packages / wtforms / fields / core.py,第204行验证
stop_validation = self._run_validation_chain(form,chain)
File /home/afidegnum/gsam/lib/python2.7/site-packages/wtforms/fields/core.py,第224行,在_run_validation_chain
验证器(form,self)
TypeError:__init __()至多2个参数(3给出)


解决方案

打电话给你的验证者:

pre $ password $ password eld('Password',validators = [DataRequired])

添加 :

  password = PasswordField('Password',validators = [DataRequired()])


I can't find out what's the cause of this error

TypeError: __init__() takes at most 2 arguments (3 given)

which is pointing to

@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)

The LoginForm looks like this:

class LoginForm(Form):
    username = StringField("Username", validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired])
    remember = BooleanField("Remember Me", default=False)

Here is the traceback:

  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)
解决方案

You didn't call one of your validators:

password = PasswordField('Password', validators=[DataRequired])

Add ():

password = PasswordField('Password', validators=[DataRequired()])

这篇关于TypeError:__init __()得到了一个意想不到的关键字参数'password'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 12:18