Django Session Data Is Not Persisting Consistently When Saving User Progress on My Morse Code Website

I am developing a Morse Code learning website using Django where users can translate text into Morse code, decode messages, and practice interactive lessons. The website also allows authenticated users to save their learning progress, recent translations, and lesson completion status. Most of the application works as expected, but I have encountered a problem with session persistence while users interact with the learning features. The issue appears only after users spend some time actively using the website.

The translator itself performs all conversions in the browser using JavaScript, while Django is responsible for authentication and storing user-specific information. When a logged-in user completes practice exercises or saves translation history, the information is temporarily available during the session. However, after continued use or after navigating between several pages, some session-related data unexpectedly disappears even though the user remains logged in. This causes progress tracking to become inconsistent.

To investigate, I verified that the session engine is configured correctly and confirmed that authentication remains valid throughout the user’s visit. Database records for user accounts remain intact, and there are no authentication errors in the logs. The problem seems limited to application data that is being stored in the session rather than permanent models. Because the behavior occurs intermittently, reproducing it consistently has been challenging.

I also reviewed middleware configuration, session expiration settings, and browser cookies to determine whether the session is being recreated unexpectedly. From what I can observe, the session cookie itself remains valid, but certain values stored in the session are no longer available after specific user actions. Since the issue only affects some session variables, I am unsure whether I am misunderstanding the intended use of Django sessions for this type of application.

The Morse Code website includes long practice sessions where users remain active for extended periods before saving their work. My goal is to maintain lightweight session data during practice while storing completed progress in the database when appropriate. Before redesigning this workflow, I would like to understand whether storing temporary learning state in Django sessions is the recommended approach or if there is a better pattern for applications with extended user interaction.

Has anyone experienced similar issues with Django session data appearing to reset or disappear while users remain authenticated? I would appreciate any guidance on best practices for session management, temporary application state, or debugging techniques that could help identify why session values are not persisting consistently in this type of interactive educational application. Sorry for long post!

Welcome @benstokes !

General thoughts - Things shouldn’t “just happen”, but there are a lot of moving parts involved. Tracking this down would likely involve tracing the activity of the storage engine during a session. (Which storage engine are you using? Have you tried using a different engine, just to see what’s happening? It might be easier to trace operations if you’re using PostgreSQL as the engine as opposed to something like memcached.

<opinion>
I believe it’s always better to be explicit. In this situation, I’d much prefer storing this data in a model.
Even if you’re effectively only recreating what the session module does by creating a Model containing a JSON field and storing all this temp data within this single field, you still have a much better chance of ensuring that the data is only updated when you want it updated.
</opinion>

Personally, I restrict using the session to items that are truly temporary - data that needs to be tracked or maintained across a small number of views in a short period of time. Once I’m to the point where I’m expecting to keep data more than just a minute or two, that’s when I start looking at a more persistent storage process.