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

发布于 2024-11-19 02:59:59 字数 1802 浏览 3 评论 0原文

我正在尝试按照此处的说明为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-11-26 02:59:59

(作为答案发布,以便您可以关闭问题)

根据文档 ,您确实需要使用 connect() 方法显式连接到 MongoDB。 这可能很简单

connect('dbname')

如果您在本地计算机上运行 MongoDB,并且还接受 hostport 的关键字参数(应该是 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 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

Also per the docs, the right place to put your call to connect() is in settings.py.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文