开发者

Auto logout using sessions in Django (outside views)

I'm trying to build a auto-logout function in a Django application.

Basically, with each request to the site I want to set the current timestamp in the session (if not set), and then checking that value with the current time. If the difference is too great, it should redirect to logout.

Is there a easy way to set the session on each request without adding a function to each 开发者_如何学编程of my views? I know it's possible to use sessions outside views, but then I have to supply the session_key, and I'm not sure where I should get it from, or generate it myself.


I'm not sure what timestamp you are comparing with what here, or why.

The usual way to manage auto-logout is to simply set a short expiry on the session cookie, via the SESSION_COOKIE_AGE setting. If the cookie expires, the user will automatically be redirected to the login page if they try and access a page that requires authentication.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜