title: Django Session Persistent but Losing Data
body: <p>I have been working for hours trying to understand the following problem: I have a user send an Ajax request to dynamically send a form and record that the number of forms to read on submission has increased. Toward this end I use <code>request.session['editing_foo'] = { 'prefix_of_form_elements' : pkey }</code> so that I can associate them with the database for saving and loading (-1 is for new forms that haven't been saved yet). </p>
  
  <p>However, when I use the following code (see bottom) I get the following bizarre output:</p>
  
  <p>1st Click:</p>
  
  <pre><code>{} foousername
  next_key 1
  1
  {u'1-foo': -1}
  </code></pre>
  
  <p>2nd Click:</p>
  
  <pre><code>{} foousername
  next_key 1
  1
  {u'1-foo': -1}
  </code></pre>
  
  <p>3rd Request:</p>
  
  <pre><code>{} foousername
  next_key 1
  1
  {u'1-foo': -1}
  </code></pre>
  
  <p>What the heck is going on?</p>
  
  <pre><code>id_fetcher = re.compile(r'\d')
  
  
  @login_required
  def ajax_add_foo(request):
      def id_from_prefix(key):
          return int( id_fetcher.search(key).group(0) )
  
      if 'editing_foos' not in request.session:
          print "reinitializing"
          request.session['editing_foos'] = {}
  
      print request.session['editing_foos'], request.user
      keys = request.session['editing_foos'].keys()
      if len(keys) == 0:
          next_key = 1
      else:
          print [ id_from_prefix(key) for key in keys ]
          next_key = max([ id_from_prefix(key) for key in keys ]) + 1
      print "next_key", next_key
  
      fooform = FooForm(prefix=next_key)
      print next_key
  
      request.session['editing_foos'].update( {create_prefix(FooForm, next_key) : -1 } ) # This quote is new and has no pkey
      print request.session['editing_foos']
  
      return render_to_response( 'bar/foo_fragment.html',
                                  {'fooform' : fooform, },
                                  context_instance=RequestContext(request))
  </code></pre>
  
  <p>Thank you all very much!</p>
  
  <p>Note: This is a followup to a <a href="https://stackoverflow.com/questions/1945182/django-development-server-not-serving-errors-from-ajax-requests">previous question</a> concerning the same source code. </p>