在 django 中:如何更新当前会话的到期日期?
我有一个用户登录。 我如何延长/更新从请求中收到的会话的到期日期? 提前致谢!
I have a user logged in.
How can i extend/renew expiry date of session received from the request ?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有必要为此制作自定义中间件。
设置
SESSION_SAVE_EVERY_REQUEST = True
将导致 Django 现有的SessionMiddleware
完全按照您的要求执行。它有这样的代码:
It's not necessary to make a custom middleware for this.
Setting
SESSION_SAVE_EVERY_REQUEST = True
will cause Django's existingSessionMiddleware
to do exactly what you want.It has this code:
这是一些扩展经过身份验证的用户会话的中间件。如果距离会话
expiry_date
不到 30 天,它会将会话延长 60 天,从而使他们保持永久登录状态。custom_middleware.py:
然后,您需要在 Django 的 SessionMiddleware 之后添加此自定义中间件,因此您的设置文件应如下所示:
project/settings.py:
Here's some middleware that extends an authenticated user's session. It essentially keeps them permanently logged in by extending their session another 60 days if their session
expiry_date
is less than 30 days away.custom_middleware.py:
You will then need to add this custom middleware after Django's SessionMiddleware, so your settings file should look like:
project/settings.py:
设置 SESSION_COOKIE_AGE 就是为此目的而设计的我相信。登录后,在此期间自动设置 cookie。
您还可以使用 SESSION_SAVE_EVERY_REQUEST 设置。
setting SESSION_COOKIE_AGE is designed for that purpose I believe. After login cookie is set automatically for this period.
You can also save session cookie on every request by using SESSION_SAVE_EVERY_REQUEST setting.