Python ConnectionError at:您尚未定义默认连接
我在运行时遇到错误: 环境: 请求方法:发布请求URL: Django版本:1.8.4 Python版本:2.7.6已安装的应用程序: ('django.contrib.admin'、'django.contrib.auth', 'django.contrib.contenttypes','django.contrib.sessions', 'django.contrib.messages'、'django.contrib.staticfiles', “mongo_登录”)已安装的中间件: ('django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', “django.middleware.csrf.CsrfViewMiddleware”, 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware') 回溯:文件 “/usr/local/lib/python2.7/dist packages/django/core/handlers/base.py” 在得到答复时 132响应=包装的回调(请求,*回调参数,**回调参数) 有效吗 184返回self.is_绑定而不是self.errors 文件“/usr/local/lib/python2.7/dist-packages/django/forms/forms.py” 错误 176self.full_clean() 文件“/usr/local/lib/python2.7/dist-packages/django/forms/forms.py” 完全清洁 392self._clean_fields() 文件“/usr/local/lib/python2.7/dist-packages/django/forms/forms.py” _清洁农田 410value=getattr(self,'clean_uu%s'%name)() 文件 “/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/manager.py” 在中获取 37queryset=queryset\u类(所有者,所有者.\u获取\u集合()) 文件“/usr/local/lib/python2.7/dist packages/mongoengine/document.py” _收集 176db=cls.\u get\u db() 文件“/usr/local/lib/python2.7/dist packages/mongoengine/document.py” _获取\u db 169返回get\u db(cls.\u meta.get(“db\u别名”,默认连接\u名称)) 文件“/usr/local/lib/python2.7/dist-packages/mongoengine/connection.py” 获取\u db 145conn=获取连接(别名) 文件“/usr/local/lib/python2.7/dist-packages/mongoengine/connection.py” 获得连接 101raise ConnectionError(消息) 代码是: View.pyPython ConnectionError at:您尚未定义默认连接,python,django,python-2.7,django-models,Python,Django,Python 2.7,Django Models,我在运行时遇到错误: 环境: 请求方法:发布请求URL: Django版本:1.8.4 Python版本:2.7.6已安装的应用程序: ('django.contrib.admin'、'django.contrib.auth', 'django.contrib.contenttypes','django.contrib.sessions', 'django.contrib.messages'、'django.contrib.staticfiles', “mongo_登录”)已安装的中间件: ('
from django.template.context import RequestContext
from django.shortcuts import redirect, render_to_response
from django.conf import settings
from forms import RegistrationForm
from auth import User as MongoUser
def save_user_in_mongo(**kwargs):
new_user = MongoUser.create_user(kwargs['username'], kwargs['password1'], kwargs['email'])
new_user.first_name = kwargs.get('first_name')
new_user.last_name = kwargs.get('last_name')
new_user.save()
return new_user
def register(request, success_url=None, form_class=None,
template_name='registration/registration_form.html',
extra_context=None):
if form_class is None:
form_class = RegistrationForm
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid(): # line 24 =======
save_user_in_mongo(**form.cleaned_data)
if success_url is None:
success_url = settings.LOGIN_URL
return redirect(success_url)
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{'form': form},
context_instance=context)
from auth import User as MongoUser
from django import forms
from django.utils.translation import ugettext_lazy as _
attrs_dict = {'class': 'required'}
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_("Username"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_("E-mail"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password (again)"))
first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("First Name"))
last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("Last Name"), required=False)
def clean_username(self):
existing = MongoUser.objects(username=self.cleaned_data['username'])#line 56 ======
if existing:
raise forms.ValidationError(_("A user with that username already exists."))
else:
return self.cleaned_data['username']
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields didn't match."))
return self.cleaned_data
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'my_database',
'OPTIONS' : {
'socketTimeoutMS' : 500,
}
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'logintest.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
forms.py
from django.template.context import RequestContext
from django.shortcuts import redirect, render_to_response
from django.conf import settings
from forms import RegistrationForm
from auth import User as MongoUser
def save_user_in_mongo(**kwargs):
new_user = MongoUser.create_user(kwargs['username'], kwargs['password1'], kwargs['email'])
new_user.first_name = kwargs.get('first_name')
new_user.last_name = kwargs.get('last_name')
new_user.save()
return new_user
def register(request, success_url=None, form_class=None,
template_name='registration/registration_form.html',
extra_context=None):
if form_class is None:
form_class = RegistrationForm
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid(): # line 24 =======
save_user_in_mongo(**form.cleaned_data)
if success_url is None:
success_url = settings.LOGIN_URL
return redirect(success_url)
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{'form': form},
context_instance=context)
from auth import User as MongoUser
from django import forms
from django.utils.translation import ugettext_lazy as _
attrs_dict = {'class': 'required'}
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_("Username"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_("E-mail"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password (again)"))
first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("First Name"))
last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("Last Name"), required=False)
def clean_username(self):
existing = MongoUser.objects(username=self.cleaned_data['username'])#line 56 ======
if existing:
raise forms.ValidationError(_("A user with that username already exists."))
else:
return self.cleaned_data['username']
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields didn't match."))
return self.cleaned_data
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'my_database',
'OPTIONS' : {
'socketTimeoutMS' : 500,
}
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'logintest.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
设置.py
from django.template.context import RequestContext
from django.shortcuts import redirect, render_to_response
from django.conf import settings
from forms import RegistrationForm
from auth import User as MongoUser
def save_user_in_mongo(**kwargs):
new_user = MongoUser.create_user(kwargs['username'], kwargs['password1'], kwargs['email'])
new_user.first_name = kwargs.get('first_name')
new_user.last_name = kwargs.get('last_name')
new_user.save()
return new_user
def register(request, success_url=None, form_class=None,
template_name='registration/registration_form.html',
extra_context=None):
if form_class is None:
form_class = RegistrationForm
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid(): # line 24 =======
save_user_in_mongo(**form.cleaned_data)
if success_url is None:
success_url = settings.LOGIN_URL
return redirect(success_url)
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{'form': form},
context_instance=context)
from auth import User as MongoUser
from django import forms
from django.utils.translation import ugettext_lazy as _
attrs_dict = {'class': 'required'}
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_("Username"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_("E-mail"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password (again)"))
first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("First Name"))
last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("Last Name"), required=False)
def clean_username(self):
existing = MongoUser.objects(username=self.cleaned_data['username'])#line 56 ======
if existing:
raise forms.ValidationError(_("A user with that username already exists."))
else:
return self.cleaned_data['username']
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields didn't match."))
return self.cleaned_data
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'my_database',
'OPTIONS' : {
'socketTimeoutMS' : 500,
}
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'logintest.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
我怎样才能解决这个问题 看来Django不知道如何连接到数据库 在
settings.py
DATABASES = {
'default' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database',
...
'OPTIONS' : {
'socketTimeoutMS' : 500,
...
}
}
}
使用django项目中已经添加的sqlite3数据库尝试此操作 更改设置。py
from django.template.context import RequestContext
from django.shortcuts import redirect, render_to_response
from django.conf import settings
from forms import RegistrationForm
from auth import User as MongoUser
def save_user_in_mongo(**kwargs):
new_user = MongoUser.create_user(kwargs['username'], kwargs['password1'], kwargs['email'])
new_user.first_name = kwargs.get('first_name')
new_user.last_name = kwargs.get('last_name')
new_user.save()
return new_user
def register(request, success_url=None, form_class=None,
template_name='registration/registration_form.html',
extra_context=None):
if form_class is None:
form_class = RegistrationForm
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid(): # line 24 =======
save_user_in_mongo(**form.cleaned_data)
if success_url is None:
success_url = settings.LOGIN_URL
return redirect(success_url)
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{'form': form},
context_instance=context)
from auth import User as MongoUser
from django import forms
from django.utils.translation import ugettext_lazy as _
attrs_dict = {'class': 'required'}
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_("Username"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_("E-mail"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password (again)"))
first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("First Name"))
last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
label=_("Last Name"), required=False)
def clean_username(self):
existing = MongoUser.objects(username=self.cleaned_data['username'])#line 56 ======
if existing:
raise forms.ValidationError(_("A user with that username already exists."))
else:
return self.cleaned_data['username']
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields didn't match."))
return self.cleaned_data
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'my_database',
'OPTIONS' : {
'socketTimeoutMS' : 500,
}
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'logintest.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
我已经尝试过了,但它给出了一个错误:没有名为django_mongodb_engine.base的模块