我的烧瓶应用程序中有一个奇怪的错误。问题是下面的代码在 Flask 开发服务器中运行良好,但在生产服务器(Uwsgi+Nginx)中失败。该代码的目的是从 mongoDb 检索整个集合并将其内容作为 JSON 提供。
我没有使用 virtualenv,因为我在虚拟机下工作,而且我没有在该机器上运行任何其他项目。所以堆栈是这样的:
Python Flask MongoDb->MongoEngine Uwsgi NginX
我有两个模型:
公司一份:
class Company(Document):
name = StringField(max_length=50)
....
一个用于活动:
class Campaign(Document):
name = StringField(min_length=8)
company = ReferenceField(Company)
....
路由是这样完成的:
@app.route('/getjson', methods=['GET', 'POST'])
def getJson():
if request.method == 'GET':
collection = request.args.get('collection')
if collection == 'Campaign':
return jsonify (getCampaign())
getCampaign() 函数是这样的:
def getCampaign():
theCollection = Campaign.objects.all()
theDict = {}
for obj in x:
dict_model = {
obj.name:
{
'company': obj.company.name,
...
}
theDict.update(dict_model)
return theDict
这会产生一个格式良好的 Json,其中“application/json”作为 Content-Type。问题是当在生产服务器上尝试相同的代码时,它会惨遭失败并抛出以下错误!
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./mobuy.py", line 58, in getJson
return jsonify (getCampaign())
File "./jsonOut.py", line 36, in getCampaign
'company': obj.company.name,
File "/usr/local/lib/python2.6/dist-packages/bson/dbref.py", line 88, in __getattr__
raise AttributeError(key)
AttributeError: name
运行 " 'company': "%s" % obj.dict " 会抛出这个结果:
"company":"{'_created': False, '_data': {'status': 3, 'startDate':
datetime.datetime(2012, 5, 23, 20, 22, 28, 42000), None: ObjectId('4fbd4704f65b813c5900000d'),
'EndDate': datetime.datetime(2012, 5, 23, 20, 22, 28, 42000),
'name': u'Campaign13', 'max_claimed': 39, 'text': u'a discount',
'company': DBRef(u'company', ObjectId('4fbd36f2f65b813869000008')),
'image': u'http://www.Company8.com/image.jpg', 'Shops': [],
'category': 5, 'id': None, 'coupons': []},
'_id': ObjectId('4fbd4704f65b813c5900000d'), '_changed_fields': [],
'_initialised': True}",
帮助!为什么这在开发服务器中工作而不在 Uwsgi 中工作?