Search code examples
djangoherokusession-cookiesdjango-authenticationheroku-postgres

Why are sessions not being persisted in my Django app?


I have a Django (1.11.8) application running on a staging server on Heroku.

I imported the database with existing records, users, etc and it all seems to be working fine.

When I sign in, I am sometimes returned to the sign in page. Other times I am signed in and I'm redirected to the user profile page. However, if I do anything after that I end up getting signed out.

I tried making sure the name of the cookies don't clash with the production application (which works fine).

I also inspected Session records via the shell. When I manage to sign in, it doesn't look like it's creating any new session records. To be sure I cleared all the session records and no new ones get created when I sign in.

The database connection seems fine, as I'm able to create a user record by signing up. I've also been able to manually create a session record via the shell. I just don't see any being created as a result of the sign in page.

Any ideas appreciated?

Edit:

So in installed apps I'm using:

'django.contrib.admin', 'django.contrib.auth', 'django.contrib.sessions'

Engine:

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

In middleware:

'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',

EDIT2: OH.

OK, so I read up on the session engine thing:

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

https://docs.djangoproject.com/en/1.11/topics/http/sessions/#using-cached-sessions

Since I'm using the cache and not the db in development that would explain why I don't see anything happening in the DB. I'm guessing there's an issue with the cache in Heroku.


Solution

  • Turns out cache storage doesn't work on Heroku, this answer cleared it all up: Django Session Not Working on Heroku