Hassan Baig
Hassan Baig

Reputation: 15824

Last activity field in Django's user sessions

How and when exactly does last_activity in Django sessions get updated? I've been testing a Django app, and my last activity in user sessions is logged as several days ago, even though I logged in yesterday as well. What could be going on?

Upvotes: 2

Views: 956

Answers (1)

e4c5
e4c5

Reputation: 53744

That's a direct result of when sessions are saved

By default, Django only saves to the session database when the session has been modified – that is if any of its dictionary values have been assigned or deleted:

If you want to mark a user as being active, you can place the following code in key areas of your app to mark the session as being modified so that it will be saved again in the storage

request.session.modified = True

Alternatively you can use SESSION_SAVE_EVERY_REQUEST to make sure that the session gets saved on each and every request this of course comes with an extra hit to the db.

Upvotes: 1

Related Questions