2015-12-01 76 views
-2

我想用Flask構建python webservice,它可以從mongo中獲取數據,並帶有http://docs.mongoengine.org/projects/flask-mongoengine/en/latest/pymngo和燒瓶的組合

我有蒙戈查詢:

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錯誤。任何建議?也想爲它添加分頁。

error while debug = 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. 

回答

1

我可以發現你忘記導入render_template

改變這一行從:

from flask import Flask 

from flask import Flask, render_template 

如果仍存在一定的誤差,打開調試模式:

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

並粘貼完整的錯誤日誌。

+0

Thanks @ lord63.j。讓我試試這個。這裏調試=真要求我運行它它是python 3.所以有什麼其他方式我可以用Python 2.7 –

+0

調試「請求我運行它它python 3」我不知道這是什麼意思?你能告訴我完整的日誌嗎? –

+0

已更新的問題 –