Python 将Django连接到MongoLab数据库

Python 将Django连接到MongoLab数据库,python,django,mongodb,pymongo,mlab,Python,Django,Mongodb,Pymongo,Mlab,我目前正在运行Django 1.4.3,我想连接我自己在MongoLab中创建的数据库,与我的Django网站进行对话。我已经尝试了很多关于如何实现这一点的教程;我在Django settings.py文件中遇到的问题是,由于无法连接,我的数据库设置不正确。我是Django和MongoDB的新手,所以如果有任何帮助,我将不胜感激 以下是我遵循的教程之一: 注:我使用的是Mac电脑,如果这很重要的话,我会用终端来编程 这里是my settings.py中的问题区域most是由Django自动生成的

我目前正在运行Django 1.4.3,我想连接我自己在MongoLab中创建的数据库,与我的Django网站进行对话。我已经尝试了很多关于如何实现这一点的教程;我在Django settings.py文件中遇到的问题是,由于无法连接,我的数据库设置不正确。我是Django和MongoDB的新手,所以如果有任何帮助,我将不胜感激

以下是我遵循的教程之一:

注:我使用的是Mac电脑,如果这很重要的话,我会用终端来编程

这里是my settings.py中的问题区域most是由Django自动生成的

DATABASES = {
    'default': {
        #'ENGINE': 'django_mongodb_engine', ### This wasn't working ###
        'ENGINE': '',
        'NAME': '',                      # Or path to database file if using sq$
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. N$
        'PORT': '',                      # Set to empty string for default. Not$
    }
}
以下是错误消息:

Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x10f732210>>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 23, in get_validation_errors
from django.db import models, connection
  File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
  File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 92, in __getitem__
backend = load_backend(db['ENGINE'])
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 24, in load_backend
return import_module('.base', backend_name)
  File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
  File "build/bdist.macosx-10.8-intel/egg/django_mongodb_engine/__init__.py", line 13, in <module>
AttributeError: 'tuple' object has no attribute 'insert'

您需要配置设置文件中的值。正如您所看到的,它们大部分是空字符串。MongoLab将在您创建的数据库主页上为您提供所需的所有连接信息

您需要配置设置文件中的值。正如您所看到的,它们大部分是空字符串。MongoLab将在您创建的数据库主页上为您提供所需的所有连接信息

似乎是个bug

检查

如果你点击链接,开发者会说:

mongodb引擎目前不支持django 1.4。所以,坚持下去 1.3

这是昨天的评论。就是这样,您使用的Django版本不兼容。

似乎是个bug

检查

如果你点击链接,开发者会说:

mongodb引擎目前不支持django 1.4。所以,坚持下去 1.3


这是昨天的评论。就是这样,您使用的Django版本不兼容。

您可以发布您的settings.py和您的完整错误吗?尽管它主要是从Djangopost错误消息自动生成的,但刚刚发布了,请您可以发布您的settings.py和您的完整错误吗?尽管它主要是从Djangopost错误消息自动生成的,请把它们拿出来,我有所有这些正确的值,我只是无法通过django_mongodb_引擎连接。我不知道为什么它无法连接。你能发布一条错误消息吗?你能处理stacktrace吗?隐藏所有密码该文件是用新项目自动创建的。该文件是完全空白/空的。关于错误消息中引用的文件msg:build/bdist.macosx-10.8-intel/egg/django\u mongodb\u engine/\uuuuu init\uuuuu.py,我发布了它,但在从教程链接下载后,我也没有修改它。我取出了它们,我有所有这些正确的值,我无法通过django_mongodb_引擎连接。我不知道为什么它无法连接。你能发布一条错误消息吗?你能处理stacktrace吗?隐藏所有密码该文件是用新项目自动创建的。该文件是完全空白/空的。关于错误消息中引用的文件msg:build/bdist.macosx-10.8-intel/egg/django\u mongodb\u engine/\uuuu init\uuuu.py,我发布了它,但在从教程链接下载后,我也没有修改它。
#!/usr/bin/python
# -*- coding: utf-8 -*-

__version__ = (0, 4, 0)
__author__ = "Flavio Percoco Premoli, Alberto Paro, " + \
         "Jonas Haag and contributors"
__contact__ = "django-non-relational@googlegroups.com"
__homepage__ = "https://django-mongodb.org"
__docformat__ = "restructuredtext"

try:
    from django.conf import settings
    settings.INSTALLED_APPS.insert(0, 'django_mongodb_engine')
    # It might be irritating that django-mongodb-engine registers itself as an app,
    # and I think this is worth an explanation - so here you go:
    # django-mongodb-engine provides a way to set MongoDB-specific options for a
    # certain model via the 'MongoMeta' class/attribute (similar to the Django-style
    # 'Meta' class).  We want those options to be copied into the model's
    # '_meta' object, right after the class has been defined.
    # For this, we have to listen to the 'class_prepared' signal from
    # 'django.db.models.signals'. Because the 'django_mongodb_engine' module gets
    # imported as part of the initialization process of Django's ORM ('django.db'),
    # we can *not* import anything from 'django.db' in this file (or any other
    # submodule that is imported while the ORM initialization) because that would
    # get us into recursive import hell which the Python interpreter doesn't allow.
    # The only way to make sure certain code is executed after Django's ORM has been
    # initialized is registering an app. After initializing itself, Django imports
    # all apps defined in the project's 'settings.py' in the order implied by
    # iterating over the INSTALLED_APPS list. As we have to make sure that
    # django-mongodb-engine is loaded very first, we prepend it to the list.
except ImportError:
pass