title: Django Session KeyError when key exists
body: <p>The following code works locally when I use Django's development server, but I am running into intermittent bugs in production with Nginx and Gunicorn.</p>
  
  <p>views.py</p>
  
  <pre><code>def first_view(request):
      if request.method == "POST":
          # not using a django form in the template, so need to parse the request POST
          # create a dictionary with only strings as values
          new_post = {key:val for key,val in request.POST.items() if key != 'csrfmiddlewaretoken'}
          request.session['new_post'] = new_mappings # save for use within next view
  
          # more logic here (nothing involving views)
          return redirect('second_view')
  
  def second_view(request):
      if request.method == 'POST':
          new_post = request.session['new_post']
          # ... more code below
          # render template with form that will eventually post to this view
  </code></pre>
  
  <p>I will sometimes receive a KeyError after posting to the second view. Based on the <a href="https://docs.djangoproject.com/en/1.11/topics/http/sessions/#when-sessions-are-saved" rel="nofollow noreferrer">documentation on when sessions are saved</a>, it seems like the session variable should be saved since it is modifying the session directly. Also, if I take the sessionid provided the error page's debug panel and access the session via Django's API, I can see the 'new_post' session variable</p>
  
  <pre><code>python manage.py shell
  &gt;&gt;&gt; from django.contrib.sessions.backends.db import SessionStore
  &gt;&gt;&gt; s = SessionStore(session_key='sessionid_from_debug_panel')
  &gt;&gt;&gt; s['new_post']
  # dictionary with expected post items
  </code></pre>
  
  <p>Is there something I'm missing? Thanks in advance for your help!</p>