error while following Tumblelog Application with Flask and MongoEngine

风格不统一 提交于 2019-12-10 03:08:59

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/29517930/error-while-following-tumblelog-application-with-flask-and-mongoengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!