用于分页的 pymongo 和 flask

pymngo and flask for pagnation

我想用 Flask 构建 python 网络服务,它可以用 http://docs.mongoengine.org/projects/flask-mongoengine/en/latest/ 从 mongo 获取数据。

我有 mongo 个查询:

db.item.find({},{"uid":1,"created_on":1,"attributes.value":1}).sort({created_on:-1}).limit(3).pretty()

输出:

{
    "_id" : ObjectId("5656f606a80ea9407853808b"),
    "uid" : 2008,
    "created_on" : ISODate("2015-11-26T05:14:07Z")
}
{
    "_id" : ObjectId("5656f5f9a80ea94076538098"),
    "uid" : 2007,
    "created_on" : ISODate("2015-11-26T05:12:29Z")
}
{
    "_id" : ObjectId("5656f5e7a80ea94076538097"),
    "uid" : 2006,
    "created_on" : ISODate("2015-11-26T05:12:20Z")
}
{
    "_id" : ObjectId("5656f5d6a80ea940775380c3"),
    "uid" : 2005,
    "created_on" : ISODate("2015-11-26T05:12:19Z")
}
{
    "_id" : ObjectId("5656f4c4a80ea94076538095"),
    "uid" : 2004,
    "created_on" : ISODate("2015-11-26T05:12:14Z")
}
{
    "_id" : ObjectId("5656f4b4a80ea940775380c2"),
    "uid" : 2003,
    "created_on" : ISODate("2015-11-26T05:12:09Z")
}
{
    "_id" : ObjectId("5656f4a7a80ea9407853808a"),
    "uid" : 2002,
    "created_on" : ISODate("2015-11-26T05:12:05Z")
}
{
    "_id" : ObjectId("5656f499a80ea940775380c1"),
    "uid" : 2001,
    "created_on" : ISODate("2015-11-26T05:12:00Z")
}
{
    "_id" : ObjectId("5656f483a80ea94078538089"),
    "uid" : 2000,
    "created_on" : ISODate("2015-11-26T05:11:55Z")
}
{
    "_id" : ObjectId("5656f462a80ea94078538088"),
    "uid" : 1999,
    "created_on" : ISODate("2015-11-26T05:11:51Z")
}
{
    "_id" : ObjectId("5656f454a80ea94075538089"),
    "uid" : 1998,
    "created_on" : ISODate("2015-11-26T05:11:46Z")
}
{
    "_id" : ObjectId("5656f42fa80ea940775380c0"),
    "uid" : 1997,
    "created_on" : ISODate("2015-11-26T05:11:42Z")
}
{
    "_id" : ObjectId("5656f404a80ea94076538094"),
    "uid" : 1996,
    "created_on" : ISODate("2015-11-26T05:11:37Z")
}
{
    "_id" : ObjectId("5656f3f7a80ea940775380bd"),
    "uid" : 1995,
    "created_on" : ISODate("2015-11-26T05:11:32Z")
}
{
    "_id" : ObjectId("5656f3d2a80ea94078538087"),
    "uid" : 1994,
    "created_on" : ISODate("2015-11-26T05:11:27Z")
}
{
    "_id" : ObjectId("5656f3bba80ea94076538093"),
    "uid" : 1993,
    "created_on" : ISODate("2015-11-26T05:11:22Z")
}
{
    "_id" : ObjectId("5656f363a80ea94078538086"),
    "uid" : 1992,
    "created_on" : ISODate("2015-11-26T05:11:17Z")
}
{
    "_id" : ObjectId("5656f351a80ea94076538092"),
    "uid" : 1991,
    "created_on" : ISODate("2015-11-26T05:11:13Z")
}
{
    "_id" : ObjectId("5656f327a80ea940775380bc"),
    "uid" : 1990,
    "created_on" : ISODate("2015-11-26T05:11:08Z")
}
{
    "_id" : ObjectId("5656f308a80ea94078538085"),
    "uid" : 1989,
    "created_on" : ISODate("2015-11-26T05:11:03Z")
}

我试过这个:

from flask import Flask
from flask.ext.pymongo import PyMongo

app = Flask(__name__)
mongo = PyMongo(app)


@app.route('/')
def home_page():
    online_items = mongo.db['mydb'].item.find({},{"uid":1}).sort({created_on:-1})
    return render_template('index.html',
        online_users=online_items)

if __name__ == '__main__':
    app.run()

但是它给出了 500 错误。有什么建议吗?还想在上面添加分页。

调试时出错 = True。

Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/local/iep/iep-3.7/source/iep/__main__.py", line 46, in <module>
    import iep
  File "/usr/local/iep/iep-3.7/source/iep/__init__.py", line 56, in <module>
    raise RuntimeError('IEP requires Python 3.x to run.')
RuntimeError: IEP requires Python 3.x to run.

我发现您忘记导入 render_template

更改此行:

from flask import Flask

from flask import Flask, render_template

如果还有一些错误,开启debug模式:

if __name__ == '__main__':
     app.run(debug=True)

并粘贴完整的错误日志。