Cannot execute load_user? when the parameter remember of load-user in flask-login is flase

problem description

I have made a Wechat scan login function. After scanning the code successfully, determine whether the user exists in the database, and if not, add a user. Then perform a login_user (user), jump to the home page. It is found that if login_user does not assign True to the remember parameter, it jumps all the way to the code scan login page. In theory, I will execute load_user after login_user (user), but I don"t. Load_user is executed only if remember = True is assigned.

the environmental background of the problems and what methods you have tried

  • python 3.7
  • Flask==1.0.2
  • Flask-Bootstrap==3.3.7.1
  • Flask-Login==0.4.1
  • Flask-Migrate==2.3.0
  • Flask-Moment==0.6.0
  • Flask-SQLAlchemy==2.3.2
  • Flask-WTF==0.14.2

related codes

/ / Please paste the code text below (do not replace the code with pictures)

  • Model models.py
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(64), index = True, unique = True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))

    about_me = db.Column(db.String(140))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)

    -sharp 20181206 
    userid = db.Column(db.String(10))
    deptid = db.Column(db.Integer, db.ForeignKey("dept.id"))

    posts = db.relationship("Post", backref="author",lazy="dynamic")
  • I never figured out what the id of this load_user is? Where did you pass it the parameters? Is it a database?
@login.user_loader
def load_user(id):
    
    print("=========load_user===========")
    return User.query.get(int(id))
  • routing auth/login
@bp.route("/login", methods = ["GET","POST"])
def login():
    if current_user.is_authenticated:
        print("authenticated")
        return redirect(url_for("main.index"))
    return render_template("auth/wxlogin.html")
  • scan the code to verify the route
@bp.route("/oauth", methods = ["GET","POST"])
def get_from_wechat():
    -sharp code 
    code = request.args.get("code")
    print("code:%s" % code)
    -sharp APIcodecode
    url = current_app.config["WECHAT_SERVER"] + "/getuser/" + code
    response = requests.get(url)
    user_json = json.loads(response.text)
    print(user_json)
    if user_json["errcode"] == 0:
        -sharp :
        user = User.query.filter_by(userid=user_json["userid"]).first()
        print("oauth:", user)
        if user is None:
            print("add user:")
            user = User(username=user_json["name"], email=user_json["userid"] + "@qq.com" )
            user.set_password("Abc123")
            user.userid = user_json["userid"]
            user.about_me = user_json["avatar"]
            user.deptid = 6
            db.session.add(user)
            db.session.commit()

        login_user(user, remember=False)
        print(current_user.is_anonymous)-sharpFalse
        return redirect(url_for("main.index"))
    else:
        return render_template("errors/404.html")

    return render_template("auth/wxlogin.html")
    -sharpreturn redirect(url_for("main.index"))
  • index.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block app_content %}
    
    <h2>{{ current_user.username }}</h2>

{% endblock %}

what result do you expect? What is the error message actually seen?

I hope that after scanning the code successfully, I will jump to the Index.html page

Jan.18,2022

print the user_loader again

@login.user_loader
def load_user(id):
    print('=========load_user===========')
    user = User.query.get(int(id))
    print('USER_LOADER', user)
    return user

found that user is a None? What on earth is this id?

Menu