'Not connected to the database' on Django admin site with MongoDB through django-nonrel
I am trying to conf开发者_C百科igure 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
(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
.
精彩评论