Error using Tumblelog Application with Flask and MongoEngine

I follow the tumbleblog app 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 an error message:

 mongoengine.connection.ConnectionError: Cannot connect to database default : False is not a read preference. 

I tried to skip in "alias"="default" in app.config["MONGODB_SETTINGS"] , but still get the same error.

+8
python flask mongodb flask-mongoengine
source share
1 answer

In your MONGODB_SETTINGS dictionary, the key for the database name should be "db", not "DB" (i.e. all lowercase letters).

The error you get is that the MongoEngine extension cannot find the "db" entry in your configuration and therefore uses "default" as the database name.

Edit

Upon further inspection, it seems like this is an error somewhere in (Flask-) MongoEngine (or possibly pymongo), where the default read_preference in mongoengine.connect is False instead of the actual read preference and does not translate into the actual default value in pymongo

If you add

 from pymongo import read_preferences 

for your import and

 'read_preference': read_preferences.ReadPreference.PRIMARY 

into your configuration dictionary, it should work (which is the default read_preference in pymongo)

+11
source share

All Articles