Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a Django session is new?

Tags:

django

session

Within a view, is there an easy way to tell if this is the very first hit in a session?

I looked for something obvious like comparing session-create-date to session-last-accessed-date, but those fields don't seem to exist.

I could set a custom key in the session data, and then if the key is missing assume it's a new session, but I wondered if there were some cleaner way.

Thanks!

like image 701
John Gordon Avatar asked Aug 28 '12 20:08

John Gordon


1 Answers

How about you calculate it via settings.SESSION_COOKIE_AGE minus my_session.get_expiry_age(). The closer to 0, the newer the session. Not sure though this is always zero for a first-hit session.

like image 189
Torsten Engelbrecht Avatar answered Oct 13 '22 04:10

Torsten Engelbrecht