会话验证和过期

Session Validation and Expiration

我目前正在阅读有关会话过期的信息,我很好奇 Django 如何知道会话是否过期?目前我已经阅读了有关在 Django 中读取和写入会话以及如何设置会话过期的信息,但在验证会话时没有遇到任何问题。使用 Django 如何知道会话是否仍然有效?

您可以使用set_expire函数设置会话的过期时间:

request.session.set_expiry(300)

您可以使用 is_authenticated 函数验证会话:

request.user.is_authenticated()