为什么预期的实例'用户'是班级?

时间:2016-11-17 14:05:39

标签: python flask

最近我正在学习Flask,并且有一个错误让我迷惑了2天:
当我尝试使用login_user登录时,它会引发TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given User中的models.py

class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(140), unique=True, index=True)
    username = db.Column(db.String(80), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    confirmed = db.Column(db.Boolean, default=False)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))


Loginform中的forms.py

class LoginForm(FlaskForm):
    email = StringField('邮箱', validators=[DataRequired(), Length(1, 64), Email])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField('记住我')
    submit = SubmitField('登陆')


/login中的views.py

@auth.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user is not None and user.verify_password(form.password.data):
            login_user(user, remember=form.remember_me.data)
            return redirect(request.args.get('next') or url_for('main.index'))
        flash('用户名或密码错误')
    return render_template('auth/login.html', form=form)


login_user中查看flask_login的源代码后,我得到位置参数user应为object,因此我运行python manage.py shell,但我得到了那些:

>>> user = User(username='aaa')
>>> type(user)
<class 'app.models.User'>
>>> user = User.query.filter_by(email='arszrc@gmail.com').first()
>>> user
<User 'Arszrc'>
>>> type(user)
<class 'app.models.User'>

有没有人会告诉我为什么user的类型是class而不是object

错误:

Traceback (most recent call last):
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/Arszrc/PycharmProjects/Flasky/app/auth/views.py", line 15, in login
    if form.validate_on_submit():
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/flask_wtf/form.py", line 161, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/wtforms/form.py", line 310, in validate
    return super(Form, self).validate(extra)
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/wtforms/form.py", line 152, in validate
    if not field.validate(self, extra):
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/wtforms/fields/core.py", line 204, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/Users/Arszrc/PycharmProjects/PyFlask3.5/lib/python3.5/site-packages/wtforms/fields/core.py", line 224, in _run_validation_chain
    validator(form, self)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given

1 个答案:

答案 0 :(得分:1)

尝试按Email

调用Email()验证程序
class LoginForm(FlaskForm):
    email = StringField('邮箱', validators=[DataRequired(), Length(1, 64), Email()])