mongodb - bottle-cork 登录后如何加载MongoDB数据库内容 'in place'?

标签 mongodb python-2.7 getjson bottle

目标

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

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

我的尝试

我想我知道解决方案中涉及的大部分“细节”,但无法将它们全部放在一起,例如:

template1.tpl

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

<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中执行登录的bottle路径和函数使用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>

路线与功能

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

简而言之

当我点击表单上的“提交”按钮时,我想修改上面 login 函数中显示的 success_redirect 值,以便存储在数据库中的 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 格式的数据,而不是“就地”加载。

最佳答案

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

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)

关于mongodb - bottle-cork 登录后如何加载MongoDB数据库内容 'in place'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20039062/

相关文章:

Python:如何优化

javascript - 为什么这个$.getJSON请求会出错?

mongodb - 函数 distinct phalcon/mvc/collection 的选项

C#:合并多数据库驱动接口(interface)

mongodb - 预定义术语映射

python - 使用 python 在 2D 列表中搜索以查找 x,y 位置

mongodb - ObjectID 和字符串版本之间的唯一区别是它更小?

python - 异常 : Cannot find PyQt5 plugin directories when using Pyinstaller despite PyQt5 not even being used

jquery - 如何用 .$getJSON 方法中的变量替换数据

javascript - 未从 JSON 文件获取数据