简体   繁体   English

Django 管理站点上的“未连接到数据库”,MongoDB 通过 django-nonrel

[英]'Not connected to the database' on Django admin site with MongoDB through django-nonrel

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/ .我正在尝试按照此处的说明为我的 django 应用程序配置 Django 管理站点: https://docs.djangoadmin/en/dev/ref/contrib.com/en/dev/ref/contrib . 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.我运行开发服务器,但是当我尝试访问/admin url 时,我收到一个堆栈跟踪错误,指出没有与数据库的连接。 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.我是 django 的新手,我假设我不需要使用 django model 层显式创建与数据库的连接。 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).我运行了manage.py syncdb ,我可以看到与 mongodb 数据库的连接有效(它在我的 mongodb 数据库中创建了几个 collections)。 So in this case it just worked.所以在这种情况下它只是工作。 What's different from the case above?与上面的案例有什么不同?

Thank you谢谢

(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.根据文档,您确实需要使用connect()方法显式连接到 MongoDB。 This can be a simple这可以是一个简单的

connect('dbname')

If you're running MongoDB on your local machine, and also accepts keyword arguments for host , port (should be an int ), username , and password , and additionally accepts the other keyword arguments that are valid for pymongo.connection.Connection如果您在本地计算机上运行 MongoDB,并且还接受关键字 arguments 用于hostport (应该是int )、 usernamepassword ,并且还接受其他关键字ZDBC11CAA5BDA99F77E6FB4DABDmongo.connection有效。

Also per the docs , the right place to put your call to connect() is in settings.py .同样根据文档,调用connect()的正确位置是在settings.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM