我正在开发一个带有mongodb后端的django-rest框架系统。当我执行命令 -
时python manage.py runserver
我收到以下错误文字:
raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))
mongoengine.connection.ConnectionError: Cannot connect to database default :
timed out
以下是该项目的相关代码' mysite':
settings.py
import os
from mongoengine import connect
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = '<xxx>'
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
MONGO_DATABASE_NAME = 'mydb'
MONGO_HOST = '<xxxx>'
MONGO_PORT = 27017
connect(MONGO_DATABASE_NAME, host=MONGO_HOST, port=MONGO_PORT)
INSTALLED_APPS = (
'mysite',
'rest_framework',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy'
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
models.py
class mydata(object):
def __init__(self, phone_no, name, address, categories):
self.phone_no = phone_no
self.name = name
self.address = address
self.categories = categories
serializers.py
from rest_framework import serializers
from models import mydata
class mydataSerializer(serializers.Serializer):
phone_no = serializers.CharField(required=True, max_length=50)
name = serializers.CharField(required=True, max_length=100)
address = serializers.CharField(required=False, max_length=200)
categories = serializers.CharField(required=False, max_length=200)
def restore_object(self, attrs, instance=None):
if instance:
instance.phone_no = attrs.get('phone_no', instance.phone_no)
instance.name = attrs.get('name', instance.name)
instance.address = attrs.get('address', instance.address)
instance.categories = attrs.get('categories', instance.categories)
return instance
return mydata(attrs.get('phone_no'),attrs.get('name'),attrs.get('address'),attrs.get('categories'))
views.py
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.response import Response
from pymongo import Connection
from models import mydata
from serializers import mydataSerializer
@csrf_exempt
@api_view(['GET'])
def pg(request):
#connect to our local mongodb
db = Connection('localhost',27017)
#get a connection to our database
dbconn = db.mydb
mydbCollection = dbconn['mydb']
if request.method == 'GET':
#get our collection
pdata = []
for r in mydbCollection.find():
temp = mydata(r["phone_no"],r["name"],r["address"],r["categories"])
pdata.append(temp)
serializedList = mydataSerializer(pdata, many=True)
return Response(serializedList.data)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^mydata/$', 'mysite.views.pg', name = 'mydata'),
url(r'^admin/', include(admin.site.urls)),
)
我创建了一个名为mydb的数据库,其中包含一个名为mydbCollection的集合,其中包含所需的数据。导致此错误的原因是什么?