title: Django sessionid disappears from cookies, user becomes anonymous
body: <p>I have developed a web application with Django, using Firebase (accessed through the Pyrebase python wrapper) as the database, and have just deployed it using Google App Engine. </p>
  
  <p>I am having issues with the sessionid, but only on the web server - not when I run the app locally.</p>
  
  <p>What happens on the web server is once the user is authenticated, the user's ID token is stored in the session dictionary via <code>request.session['uid'] = idToken</code>. When the user is directed to the home page, the app tries to retrieve idToken from the sessions dictionary, which sometimes works and sometimes doesn't. When it does work, it loads some user data on the home page, but when the user clicks on another page, it continues to randomly work or not. </p>
  
  <p>A couple things I noticed are:</p>
  
  <p>When I watch the storage section of the developer console, I see that the session storage and local storage remain empty, while Cookies carries the sessionid and csrftoken variables. Every time a new page is loaded, the sessionid is set to a new value - this doesn't happen when I run it locally.</p>
  
  <p>Eventually, the sessionid disappears from Cookies, and the error in the console reports that the user is actually anonymous. So either the user logins in and automatically is set to an anonymous user, or logs in and within a few clicks is set to an anonymous user.</p>
  
  <p>I've tried changing the session engine/storage settings, but nothing seems to work..</p>
  
  <p>The relevant parts of the error message are listed below, which is caused when the user logins in and tries to retrieve the 'uid' variable from sessions:</p>
  
  <pre><code>KeyError at /dashboard/loaduserdata
  'uid'
  
  Traceback:
  
  File "/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
    35.             response = get_response(request)
  
  File "/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
    128.                 response = self.process_exception_by_middleware(e, request)
  
  File "/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
    126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
  
  File "/home/vmagent/app/dashboard/views.py" in loaduserdata
    64.       idtoken = request.session['uid']
  
  File "/env/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py" in __getitem__
    55.         return self._session[key]
  
  Exception Type: KeyError at /dashboard/loaduserdata
  Exception Value: 'uid'
  Request information:
  USER: AnonymousUser
  
  GET: No GET data
  
  POST: No POST data
  
  FILES: No FILES data
  
  INSTALLED_APPS = ['dashboard', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles']
  
  SESSION_CACHE_ALIAS = 'default'
  SESSION_COOKIE_AGE = 1209600
  SESSION_COOKIE_DOMAIN = None
  SESSION_COOKIE_HTTPONLY = True
  SESSION_COOKIE_NAME = 'sessionid'
  SESSION_COOKIE_PATH = '/'
  SESSION_COOKIE_SECURE = True
  SESSION_ENGINE = 'django.contrib.sessions.backends.db'
  SESSION_EXPIRE_AT_BROWSER_CLOSE = False
  SESSION_EXPIRE_SECONDS = 3600
  SESSION_FILE_PATH = None
  SESSION_SAVE_EVERY_REQUEST = True
  SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
  </code></pre>
  
  <p>Any help is greatly appreciated, thanks!</p>