使用Flask和MongoEngine跟踪Tumblelog应用程序时出错 [英] error while following Tumblelog Application with Flask and MongoEngine
问题描述
我正在关注tumbleblog应用程序此处
p>我的 __ init __。py
:
from flask import Flask
from flask.ext.mongoengine import MongoEngine
$ b app = Flask(__ name__)
app.config [MONGODB_SETTINGS] = {' DB':sencha_web_service,username:< username>,password:< password>}
app.config [SECRET_KEY] =KeepThisS3cr3t
db = MongoEngine(app)
$ b $ if if __name__ =='__main__':
app.run()
我得到错误:
mongoengine.connection.ConnectionError:Can not connect到数据库默认值:
False不是读取首选项。
我尝试传入 $ alias=default
in app.config [MONGODB_SETTINGS]
但是仍然收到相同的错误。在您的MONGODB_SETTINGS字典中,数据库名称的关键字应该是'db',而不是'DB'(即全部小写)。$ b $ div class =h2_lin>解决方案
$ b
您得到的错误是因为MongoEngine扩展在您的配置中找不到'db'项,所以使用'default'作为数据库名称。
编辑
经过进一步的检查,似乎这是(Flask-)MongoEngine(或可能的pymongo),其中mongoengine.connect中的read_preference的默认值为False,而不是实际的读取首选项,并且不会转换为pymongo中的实际默认值
如果添加
from pymongo import read_preferences
来您的进口和
'read_preference':read_preferences.ReadPreference.PRIMARY
到你的配置字典,它应该可以工作(这是pymongo的默认read_preference)
I am following tumbleblog application here
my __init__.py
:
from flask import Flask
from flask.ext.mongoengine import MongoEngine
app = Flask(__name__)
app.config["MONGODB_SETTINGS"] = {'DB': "sencha_web_service", 'username': "<username>", "password": "<password>"}
app.config["SECRET_KEY"] = "KeepThisS3cr3t"
db = MongoEngine(app)
if __name__ == '__main__':
app.run()
I get the error:
mongoengine.connection.ConnectionError: Cannot connect to database default :
False is not a read preference.
I tried passing in "alias"="default"
in app.config["MONGODB_SETTINGS"]
but still getting the same error.
In your MONGODB_SETTINGS dictionary, the key for the database name should be 'db', not 'DB' (i.e. all lowercase).
The error you're getting is because the MongoEngine extension cannot find the 'db' entry in your configuration, and so uses 'default' as the database name.
Edit
Upon further inspection, it seems this is a bug somewhere in (Flask-)MongoEngine (or possible pymongo) where the default value of read_preference in mongoengine.connect is False instead of an actual read preference, and is not transformed to the actual default in pymongo
If you add
from pymongo import read_preferences
to your imports and
'read_preference': read_preferences.ReadPreference.PRIMARY
to your config dictionary, it should work (that's the default read_preference in pymongo)
这篇关于使用Flask和MongoEngine跟踪Tumblelog应用程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!