“未连接到数据库”通过 django-nonrel 在 Django 管理站点上使用 MongoDB
我正在尝试按照此处的说明为我的 django 应用程序配置 Django 管理站点: https://docs.djangoproject.com/en/dev/ref/contrib/admin/ 。 我运行开发服务器,但当我尝试访问 /admin
url 时,出现堆栈跟踪错误,表明没有与数据库的连接。我是 django 的新手,因为我假设我不需要使用 django 模型层显式创建与数据库的连接。我缺少什么?
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
178. response = middleware_method(request, response)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/middleware.py" in process_response
36. request.session.save()
File "/usr/local/lib/python2.6/dist-packages/mongoengine/django/sessions.py" in save
48. s = MongoSession(session_key=self.session_key)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py" in _get_session_key
175. self._session_key = self._get_new_session_key()
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py" in _get_new_session_key
167. if not self.exists(session_key):
File "/usr/local/lib/python2.6/dist-packages/mongoengine/django/sessions.py" in exists
34. return bool(MongoSession.objects(session_key=session_key).first())
File "/usr/local/lib/python2.6/dist-packages/mongoengine/queryset.py" in __get__
1151. db = _get_db()
File "/usr/local/lib/python2.6/dist-packages/mongoengine/connection.py" in _get_db
45. raise ConnectionError('Not connected to the database')
Exception Type: ConnectionError at /admin/
Exception Value: Not connected to the database
我运行了manage.pysyncdb
,我可以看到与 mongodb 数据库的连接正常(它在我的 mongodb 数据库中创建了多个集合)。所以在这种情况下它就起作用了。与上面的案例有什么不同?
谢谢
I am trying to configure the Django admin site for my django application by following the instructions here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/ .
I run the development server but when I try to access /admin
url I get an error with a stack trace saying that there is no connection to the database. I'm new to django by I was assuming that I didn't need to explicitly create a connection to the database by using the django model layer. What am I missing?
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
178. response = middleware_method(request, response)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/middleware.py" in process_response
36. request.session.save()
File "/usr/local/lib/python2.6/dist-packages/mongoengine/django/sessions.py" in save
48. s = MongoSession(session_key=self.session_key)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py" in _get_session_key
175. self._session_key = self._get_new_session_key()
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py" in _get_new_session_key
167. if not self.exists(session_key):
File "/usr/local/lib/python2.6/dist-packages/mongoengine/django/sessions.py" in exists
34. return bool(MongoSession.objects(session_key=session_key).first())
File "/usr/local/lib/python2.6/dist-packages/mongoengine/queryset.py" in __get__
1151. db = _get_db()
File "/usr/local/lib/python2.6/dist-packages/mongoengine/connection.py" in _get_db
45. raise ConnectionError('Not connected to the database')
Exception Type: ConnectionError at /admin/
Exception Value: Not connected to the database
I ran manage.py syncdb
and I can see that the connection to the mongodb database works (it created several collections in my mongodb database). So in this case it just worked. What's different from the case above?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(作为答案发布,以便您可以关闭问题)
根据文档 ,您确实需要使用
connect()
方法显式连接到 MongoDB。 这可能很简单如果您在本地计算机上运行 MongoDB,并且还接受
host
、port
的关键字参数(应该是int)、
用户名
和密码
,并另外接受对 pymongo.connection.Connection也按 文档,调用的正确位置
connect()
位于settings.py
中。(Posting as an answer so you can close the question)
Per the docs, you do need to explicitly connect to MongoDB using the
connect()
method. This can be a simpleIf you're running MongoDB on your local machine, and also accepts keyword arguments for
host
,port
(should be anint
),username
, andpassword
, and additionally accepts the other keyword arguments that are valid for pymongo.connection.ConnectionAlso per the docs, the right place to put your call to
connect()
is insettings.py
.