Home  >  Q&A  >  body text

python - Error when using login_user in flask-login?

flask-login is used for login verification in flask. The following is the User model:

class User(db.Model, UserMixin):
    id = db.Column(db.INTEGER, primary_key=True)
    name = db.Column(db.String(64))
    email = db.Column(db.String(64))
    password_hash = db.Column(db.String(128))

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<User %r>' % self.name


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

This is the form:

class RegisterForm(FlaskForm):
    email = StringField('Email', validators=[Required(), Length(1, 64), Email()])
    username = StringField('Username', validators=[Required(), Length(1, 16), Regexp('^[A-Za-z][1-9]*$',
                                                                                     message='first is a string and the second must be a int')])
    password = PasswordField('Password', validators=[Required(), Length(1, 64)])
    password2 = PasswordField('Config Password', validators=[Required(), Length(1, 64)])
    submit = SubmitField('Register')

    def validata_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('Email is already register')

    def validata_username(self, field):
        if User.query_by(username=field.data).first():
            raise ValidationError('User is already in use')

This is the view function:

@auth.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        user = User(name=form.username.data, email=form.email.data, password=form.password.data)
        db.session.add(user)
        login_user(user, True)
        flash('Register successful!!')
        return render_template('user.html')
    return render_template('register.html', form=form)

The current situation is that when the user successfully registers, he will automatically log in and jump to the following page, but if he continues to request after the jump, an error will occur. If the login is removed during registration, there will be no problem. I don’t know why

怪我咯怪我咯2686 days ago885

reply all(1)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-12 09:27:19

    It is None when user_id cannot be found, and an error occurs when executing int(user_id)

    Try this paragraph

    def load_user(user_id):
        return User.query.get(int(user_id))
    

    changed to

    def load_user(user_id):
        try: 
            output = User.query.get(int(user_id))
            return(output)
        except:
            return(None) 
    

    reply
    0
  • Cancelreply