如何在瓶塞登录后"就地"加载MongoDB数据库内容?

use*_*287 6 getjson mongodb bottle python-2.7

目标

用户登录并在成功授权后,在登录表单所在的同一位置(从MongoDB数据库)加载管理页面:

登录表单>提交[成功]>从表格所在的同一位置的数据库加载的内容

我试过的

我想我知道解决方案中涉及的大部分'位',但是还没能将它们全部放在一起,例如:

template1.tpl

这是一个view包含jQuery 的瓶子,它用于getJSON()与包含route查询MongoDB数据库的瓶子的Python文件(基于点击的元素'href'值)进行通信,因此返回动态内容:

<script>
function loadContent(href){
$.getJSON("/my_route", {cid: href, format: 'json'}, function(results){  
$("#content_area").html("");
$("#content_area").append(results.content);
});
}
</script>
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

登录功能

在瓶子中进行登录的瓶子路线和功能使用瓶塞,可以在这里看到.它包括:

表格

<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>
Run Code Online (Sandbox Code Playgroud)

路线和功能

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')    
Run Code Online (Sandbox Code Playgroud)

简而言之

当我单击表单上的"提交"按钮时,我想修改上面函数中success_redirect显示的值login,以便admin存储在数据库中的页面"就地"加载,即表单所在的位置.

我虽然以某种方式重定向到my_route我已经定义的函数(见my_application.py上文),并以某种方式包括一个虚拟hrefadmin,它将通知数据库查询,但我不知道如何1)传递该href值,2)使内容加载在表单所在的位置异步.

更新

以下作为值success_redirect(使用bottle.redirect)的值:

success_redirect='/my_route?cid=%2Fpage-from-db&format=json'

但它只是将json格式的数据加载到自己的网页中,而不是"就地".

use*_*287 2

我研究并在这里发布了一个解决方案,但不确定它是否是最有效的:

/sf/answers/1408240151/

形式

<form name="login" id="login">
<p>username</p>
<p>password</p>
<input type="text" name="username" />
<input type="password" name="password" />
<button type="submit">login</button>
</form>
Run Code Online (Sandbox Code Playgroud)

jQuery

<script>
$(document).on("submit","#login", function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '/login',
data: $(this).serialize(),
dataType: 'json',
success: function(results) {
$("#content_area").html("");
$("#content_area").append(results.content);
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)

Python

@post('/login')
def login():
    """Authenticate users"""
    username = post_get('username')
    password = post_get('password')
    aaa.login(username, password, fail_redirect='/login')
    dbname = 'mydb'
    connection = pymongo.MongoClient(os.environ['OPENSHIFT_MONGODB_DB_URL'])
    db = connection[dbname]
    collection = db.myCollection
    href = 'my-title'
    query = {'title':href}
    projection = {'_id':0,'content':1}
    cursor = collection.find_one(query,projection)
    response.content_type = 'application/json'
    return dumps(cursor)
Run Code Online (Sandbox Code Playgroud)