login_user、logout_user 与会话
让用户登录和退出会话
login_user、logout_user 与会话 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Logging Someone In
Once a password checks out, you tell Flask-Login the user is signed in by calling login_user. That single call sets up the session for you.
from flask_login import login_user
login_user(user)A Typical Login Route
In a login view you find the user, verify the password, then call login_user. After that, redirect them to a real page like the dashboard.
if user and user.check_password(pw):
login_user(user)
return redirect(url_for("dashboard"))What login_user Stores
login_user does not save the whole object. It writes the user id into the session cookie, and your user_loader rebuilds the user later.
Remember Me
Pass remember=True to keep the user logged in after the browser closes. Flask-Login sets a long-lived cookie instead of a session-only one.
login_user(user, remember=True)Accessing current_user
Anywhere in a view you can read current_user to get the logged-in person. It is a proxy that always points at the active request's user.
from flask_login import current_user
name = current_user.usernameAnonymous Users
If nobody is logged in, current_user is an anonymous user object. Its is_authenticated is False, so you can branch on that safely.
if current_user.is_authenticated:
show_dashboard()Logging Out
To sign a user out, call logout_user. It clears their id from the session so the next request treats them as anonymous again.
from flask_login import logout_user
logout_user()A Logout Route
A logout view is tiny: call logout_user, then redirect home. There is no password to check, only the session to clear.
@app.route("/logout")
def logout():
logout_user()
return redirect(url_for("index"))Secret Key Required
Sessions ride in a signed cookie, so your app needs a SECRET_KEY. Without it, login_user raises an error and nothing works.
app.config["SECRET_KEY"] = "a-long-random-value"Sessions Are Per-User
Each browser gets its own signed cookie, so two users never see each other's session. Flask-Login keeps them cleanly separated.
Greeting the User
In templates you can use current_user too, since Flask-Login injects it. That makes a personalized navbar a one-line job.
<p>Hello, {{ current_user.username }}</p>Quick Check
A user clicks Log out. Which call ends their session?
Recap
You now login_user after a password check, read current_user anywhere, and call logout_user to end a session. A SECRET_KEY ties it together. 🔑
常见问题解答
「login_user、logout_user 与会话」课时是免费的吗?
是的 — 「login_user、logout_user 与会话」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「login_user、logout_user 与会话」这节课中我会学到什么?
让用户登录和退出会话 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「login_user、logout_user 与会话」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 哈希密码,绝不存储明文
- 用户加载器与 UserMixin
- login_user、logout_user 与会话
- 使用 login_required 保护视图