goal
The user subscribes and, after successful authorization, the administration page is loaded (from the MongoDB database) in the same place where the login form was:
Login form> Submit [successful]> Content downloaded from the database in the same place where the form was
What i tried
I think that I know most of the "bits" that will be involved in the solution, but could not put them all together, for example:
template1.tpl
This is a view bottle that contains jQuery, it uses getJSON() to communicate with a Python file that contains a route bottle that queries the MongoDB database (based on the href 'value with click elements) and therefore returns dynamic content:
<script> function loadContent(href){ $.getJSON("/my_route", {cid: href, format: 'json'}, function(results){ $("#content_area").html(""); $("#content_area").append(results.content); }); } </script>
my_application.py
@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)
Input function
The route of the bottle and the function that enters the bottle uses bottle-cork and can be seen. It consists of:
The form
<form action="login" method="post" name="login"> <input type="text" name="username" /> <input type="password" name="password" /> <br/><br/> <button type="submit" > OK </button> <button type="button" class="close"> Cancel </button> </form>
Route and Functions
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')
In a nutshell
When I click the "Submit" button on the form, I want to change the success_redirect value shown above in the login function so that the admin page stored in the database is loaded "in place" i.e. where is the form.
I at least somehow redirect to the my_route function that I already defined (see my_application.py above) and somehow includes the dummy href admin value that will inform the database request, but I did not know how 1) pass the href value through and 2) make asynchronous loading of the content in the place where the form was.
Update
The following works as the value of success_redirect (which uses bottle.redirect ):
success_redirect='/my_route?cid=%2Fpage-from-db&format=json'
But it just loads the data in json format on its own web page, and not "in place".