在MongoDB教程(http://django-mongodb-engine.readthedocs.org/en/latest/tutorial.html)之后,我在名为'教程的应用程序中有一个名为Post的模型。'我安装了应用程序以及djangotoolbox:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'djangotoolbox',
'tutorial'
)
我有以下连接设置,其中包含在MongoLab上设置的名称,用户和密码:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': '*****',
'USER': '*****',
'PASSWORD': '******',
'HOST': 'ds047720.mongolab.com',
'PORT': '47720'
}
}
当我通过SSH连接到我的主机时,我可以运行manage.py验证,然后发现错误。" 0错误。当我进入shell并按照教程创建一个新帖子时,会发生这种情况:
(InteractiveConsole)
>>> from tutorial.models import Post
>>> post = Post.objects.create(
... title='123',
... text='test',
... tags=['a','b']
... )
Traceback (most recent call last):
File "<console>", line 4, in <module>
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/manager.py", line 149, in create
return self.get_query_set().create(**kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/query.py", line 416, in create
obj.save(force_insert=True, using=self.db)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/base.py", line 548, in save
force_update=force_update, update_fields=update_fields)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/base.py", line 668, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/query.py", line 1675, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", line 592, in execute_sql
key = self.insert(to_insert, return_id=return_id)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/compiler.py", line 84, in wrapper
return func(*args, **kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/compiler.py", line 387, in insert
collection = self.get_collection()
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/compiler.py", line 296, in get_collection
return self.connection.get_collection(self.query.get_meta().db_table)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 196, in get_collection
collection = self.collection_class(self.database, name, **kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 204, in __getattr__
self._connect()
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 261, in _connect
self.connection = connection_class(**conn_options)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 369, in __init__
raise ConnectionFailure(str(e))
DatabaseError: timed out
>>>
显然,Django很难连接到数据库,但是我能够使用Mongo Management Studio进行连接。我在这里阅读了类似的教程:http://compsocsci.blogspot.com/2012/02/getting-started-with-django-heroku-and.html 和这里: http://dennisgurnick.com/2010/07/06/bootstrapping-a-django-with-mongo-project/
但我不知道是什么阻止django连接到我的数据库。有没有人用mongodb引擎克服类似的问题?有谁知道我错过了什么?