title: How to load MongoDB database content 'in place' after bottle-cork login?
body: <p><strong>Goal</strong></p>
  
  <p>User signs in and, upon successful authorisation, an administration page is loaded (from a MongoDB database) in the same place where the login form was eg:</p>
  
  <p>Login Form > Submit [successful] > Contents Loaded from Database In Same Place Where Form Was</p>
  
  <p><strong>What I've Tried</strong></p>
  
  <p>I think I know most of the 'bits' that would be involved in the solution, but haven't been able to put them all together, for example:</p>
  
  <p><strong>template1.tpl</strong>  </p>
  
  <p>This is a bottle <code>view</code> that contains jQuery, it uses <code>getJSON()</code> to communicate with a Python file that contains a bottle <code>route</code> that queries a MongoDB database (based on the clicked elements 'href' value) and therefore returns dynamic content:</p>
  
  <pre><code>&lt;script&gt;
  function loadContent(href){
  $.getJSON("/my_route", {cid: href, format: 'json'}, function(results){  
  $("#content_area").html("");
  $("#content_area").append(results.content);
  });
  }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>my_application.py</strong></p>
  
  <pre><code>@route('/my_route')
  def my_function():
  
      # set up connection
      dbname = 'my_db_name'
      connection = pymongo.MongoClient(os.environ['OPENSHIFT_MONGODB_DB_URL'])
      db = connection[dbname]
  
      # get the data sent from the getJSON() request 
      href = request.GET.cid
  
      # specify collection based on href value
      if href =="x" or href=="y" or href=="z":
      collection = db.collection1
      elif href=="j":
      collection = db.collection2
      else:
      collection = db.collection3
  
      # define the query
      query = {'title':href}
  
      # what to return
      projection = {'_id':0,'content':1}
  
      cursor = collection.find_one(query,projection)
  
      response.content_type = 'application/json'
      return dumps(cursor)
  </code></pre>
  
  <p><strong>Login Function</strong></p>
  
  <p>The bottle route and function that performs the login in bottle utilises <a href="https://github.com/FedericoCeratto/bottle-cork" rel="noreferrer">bottle-cork</a> and can be seen <a href="https://github.com/FedericoCeratto/bottle-cork/blob/master/examples/simple_webapp_using_mongodb.py#L60" rel="noreferrer">here</a>.  It consists of:</p>
  
  <p><strong>The Form</strong></p>
  
  <pre><code>&lt;form action="login" method="post" name="login"&gt;
  &lt;input type="text" name="username" /&gt;
  &lt;input type="password" name="password" /&gt;
  
  &lt;br/&gt;&lt;br/&gt;
  &lt;button type="submit" &gt; OK &lt;/button&gt;
  &lt;button type="button" class="close"&gt; Cancel &lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>The Route &amp; Functions</strong></p>
  
  <pre><code>def post_get(name, default=''):
      return bottle.request.POST.get(name, default).strip()
  
  @bottle.post('/login')
  def login():
      """Authenticate users"""
      username = post_get('username')
      password = post_get('password')
      aaa.login(username, password, success_redirect='/', fail_redirect='/login')    
  </code></pre>
  
  <p><strong>In A Nutshell</strong></p>
  
  <p>When I click the 'Submit' button on the form I want to modify the <code>success_redirect</code> value shown in the <code>login</code> function above so that the <code>admin</code> page, as stored in the database, is loaded 'in place' ie where the form is.  </p>
  
  <p>I though about somehow redirecting to the <code>my_route</code> function I have already defined (see <code>my_application.py</code> above) and somehow including a dummy <code>href</code> value of <code>admin</code> which would inform the database query but I didn't know how to 1) pass that <code>href</code> value through and 2) make the content load asynchronously in the place where the form was.  </p>
  
  <p><strong>Update</strong></p>
  
  <p>The following works as a value of <code>success_redirect</code> (which is using <a href="http://bottlepy.org/docs/dev/tutorial.html#http-errors-and-redirects" rel="noreferrer">bottle.redirect</a>):</p>
  
  <p><code>success_redirect='/my_route?cid=%2Fpage-from-db&amp;format=json'</code></p>
  
  <p>But it just loads the data in json format in its own web page, and not 'in place'.  </p>