0

我是初学者,我在本地开发了一个简单的应用程序,它使用 mongodb 和 mongoKit,如下所示:

app = Flask(__name__)
app.config.from_object(__name__)

customerDB = MongoKit(app)
customerDB.register([CustomerModel])

然后在视图中我只使用 CustomerDB

我已将所有内容都放在 heroku 云上,但我的数据库连接不起作用。

我得到了我需要连接的链接:

heroku config | grep MONGOLAB_URI 

但我不知道如何拉这个。我看了下面的帖子,但我更困惑 如何从 python 使用 mongolab 插件到 Heroku?

任何帮助,将不胜感激。

谢谢!

4

1 回答 1

1

根据文档,Flask-MongoKit 支持一组配置设置。

MONGODB_DATABASE
MONGODB_HOST
MONGODB_PORT
MONGODB_USERNAME
MONGODB_PASSWORD

MONGOLAB_URI需要解析环境设置以获取其中的每一个。我们可以将此答案作为您链接到的问题的起点。

import os
from urlparse import urlsplit

from flask import Flask
from flask_mongokit import MongoKit

app = Flask(__name__)

# Get the URL from the Heroku setting.
url = os.environ.get('MONGOLAB_URI', 'mongodb://localhost:27017/some_db_name')
# Parse it.
parsed - urlsplit(url)

# The database name comes from the path, minus the leading /.
app.config['MONGODB_DATABASE'] = parsed.path[1:]

if '@' in parsed.netloc:
    # If there are authentication details, split the network locality.
    auth, server = parsed.netloc.split('@')
    # The username and password are in the first part, separated by a :.
    app.config['MONGODB_USERNAME'], app.config['MONGODB_PASSWORD'] = auth.split(':')
else:
    # Otherwise the whole thing is the host and port.
    server = parsed.netloc

# Split whatever version of netloc we have left to get the host and port.
app.config['MONGODB_HOST'], app.config['MONGODB_PORT'] = server.split(':')

customerDB = MongoKit(app)
于 2014-10-28T21:13:29.543 回答