Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask mongoengine connect through uri

I have a small flask application which I am deploying to Heroku.

My local configuration looks like this:

from flask import Flask
from flask.ext.mongoengine import MongoEngine

app = Flask(__name__)
app.debug = True
app.config["MONGODB_SETTINGS"] = {'DB': "my_app"}
app.config["SECRET_KEY"] = "secretpassword"

db = MongoEngine(app)

So, I know that I need to configure the app to use the Mongo URI method of connection, and I have my connection info:

mongodb://<user>:<password>@alex.mongohq.com:10043/app12345678

I am just a little stuck as to the syntax for modifying my app to connect through the URI.

like image 868
Darwin Tech Avatar asked Mar 27 '13 18:03

Darwin Tech


2 Answers

So I got it working (finally):

from flask import Flask
from mongoengine import connect

app = Flask(__name__)

app.config["MONGODB_DB"] = 'app12345678'
connect(
    'app12345678',
    username='heroku',
    password='a614e68b445d0d9d1c375740781073b4',
    host='mongodb://<user>:<password>@alex.mongohq.com:10043/app12345678',
    port=10043
)

Though I anticipate that various other configurations will work.

like image 72
Darwin Tech Avatar answered Oct 25 '22 00:10

Darwin Tech


When you look at the flask-mongoengine code, you can see what configuration variables are available

So this should work:

app.config["MONGODB_HOST"] = 'alex.mongohq.com/app12345678'
app.config["MONGODB_PORT"] = 10043
app.config["MONGODB_DATABASE"] = 'dbname'
app.config["MONGODB_USERNAME"] = 'user'
app.config["MONGODB_PASSWORD"] = 'password'
db = MongoEngine(app)

I'm not sure, if app123 is the app or the database name. You might have to fiddle arround a little to get the connection. I had the same problem with Mongokit + MongoLab on Heroku :)

Also you could use the URI like this.

app.config["MONGODB_SETTINGS"] = {'DB': "my_app", "host":'mongodb://<user>:<password>@alex.mongohq.com:10043/app12345678'}

I have actually no idea, at what point "MONGODB_SETTINGS" is read, but it seemed to work, when I tried it in the shell.

like image 28
Smoe Avatar answered Oct 24 '22 23:10

Smoe