Take the 2-minute tour ×

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.


share|improve this question
possible duplicate of Django - detect session start and end –  ire_and_curses Aug 28 '12 at 20:26
It doesn't seem to be a duplicate. –  John Gordon Aug 28 '12 at 20:30
add comment

1 Answer

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.

share|improve this answer
Yeah, I thought about that too. But like you say, the difference isn't guaranteed to be zero for a new session. –  John Gordon Aug 29 '12 at 16:01
Except this I don't see a built-in way to do it. You might be able to find a workaround, extending session model (db sessions) or saving session creation somewhere else. Another idea is to save a flag in the session (on creation), a boolean or something which you toggle first time the session is used. –  Torsten Engelbrecht Aug 30 '12 at 1:46
add comment

Your Answer


