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

时间:2013-11-18 02:25:58

标签: mongodb python-2.7 getjson bottle

目标

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

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

我尝试过什么

我想我知道解决方案中涉及的大部分“比特”,但未能将它们全部放在一起,例如:

template1.tpl

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

<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)

登录功能

在瓶子中执行登录的瓶子路线和功能使用bottle-cork,可以看到here。它包括:

表格

<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>

路线&amp;功能

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')    

坚果壳

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

我虽然以某种方式重定向到我已定义的my_route函数(请参阅上面的my_application.py)并以某种方式包含href的虚拟admin值,这将告知数据库查询,但我不知道如何1)传递href值和2)使内容在表单所在的位置异步加载。

更新

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

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

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

1 个答案:

答案 0 :(得分:2)

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

https://stackoverflow.com/a/20117716/1063287

<强>表格

<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>

<强>的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>

<强>的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)