Django Key Based End Session

I have a website that contains several sets of rules for different users. One of the rules (permission) depends on the expiration of the session.

For example, a session of non-authenticated users should be cleared if the browser is closed, however authenticated user sessions should live for a constant time.

In addition, for authenticated users, some keys in a session can be deleted when the browser is closed, but others must be saved.

How can I achieve this key based session end in Django?

+4
source share
1 answer

You can do this using the set_expiry method on request.session . The method accepts either an integer within a few seconds to expire a session, and datetime or timedelta , when the session expires, an integer 0 indicating that the session should expire during browser close time or None to indicate that the session should return to the default timeout policy.

You should be able to write a piece of middleware that evaluates the criteria that you have to end the session, and then call set_expiry in the session before processing the request.

0
source

All Articles