我正在尝试学习 python、mongodb 和烧瓶,并且正在使用 Miguel Grinberg 的非常优秀的博客,他在 blog.miguelgrinberg.com 上提供了一组很棒的教程
我有一个小型 RESTful 服务器工作正常,但现在想从 mongo 而不是 mysql 中提取东西
我可以使用下面的代码拉出一个 mongo 记录,但我正在努力让它呈现。
我在下面的代码中使用了箭头来显示我在哪里挣扎,我认为缺乏经验。任何想法将不胜感激。
#!flask/bin/python
from flask import Flask, jsonify, abort, make_response, url_for
from pymongo import MongoClient
# connect to mongo database hosted on AWS
# the script expects the host name to be in /etc/hosts file
'''
Set up global variables here
'''
mongo_server = "mongo_api"
mongo_port = "27017"
mongo_user = "admin"
mongo_passwd = ":mysecretpassword@"
connect_string = "mongodb://"+ mongo_user
+ mongo_passwd
+ mongo_server
+ ":"
+ mongo_port
app = Flask(__name__)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify( { 'error': 'Notfound' } ), 404)
def make_public_page(page):
new_page = {}
for field in page:
if field == 'id':
new_page['uri'] = url_for('get_page', page_id = page['id'], _external = True)
else:
new_page[field] = page[field]
return new_page
@app.route('/api/v1.0/pages/<int:page_id>',methods = ['GET'])
def get_page(page_id):
'''
Can connect otherwise exit with message
'''
try:
connection = MongoClient(connect_string) # equal to > show dbs
except:
exit("Error: Unable to connect to the database") # exit with an error
'''
connect to database and pull back collections
'''
db = connection.test_database # equal to > use test_database
pages = db.pages
page = pages.find_one({"id": int(page_id)}) <------ this pulls back a document
if page == None: <---- if a null set comes back then this works great
abort(404)
return jsonify( { 'page' : make_public_page(page[0])} ) <- error says its not json
if __name__ == '__main__':
app.run(debug = True)
任何帮助表示赞赏,page[0] 是无法正常工作的代码,我得到了
TypeError: ObjectId('527e17c538320915e9893f17') 不是 JSON 可序列化的
提前致谢
顺便说一句,不能推荐 Miguel 的大型教程作为开始构建东西的地方