Arszrc Arszrc - 18 days ago 6
Python Question

Why the expected instance 'user' is a class?

recently I'm learning Flask, and there is a bug confused me for 2 days:
When I try to login in with

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


The
User
in
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'))




The
Loginform
in
forms.py
:


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




The
/login
in
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)





After viewing the source code of
login_user
in
flask_login
,I got that the positional argument
user
should be a
object
,so I run
python manage.py shell
,But I got those:

>>> 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'>


Is there anyone who would tell me why the type of
user
is
class
instead of
object
?

Error:

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

Answer

Try to call the Email validator by Email():

class LoginForm(FlaskForm):
    email = StringField('邮箱', validators=[DataRequired(), Length(1, 64), Email()])
Comments