我正在尝试使用django,mongodb。
实现自定义用户身份验证和授权应用程序设置会话变量时我收到连接错误。我无法弄清楚为什么会这样。请帮我弄明白。
Setting.py
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
SESSION_ENGINE = 'mongoengine.django.sessions'
# AUTH_USER_MODEL = 'mongo_auth.MongoUser'
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
'django.contrib.auth.context_processors.auth',
)
INSTALLED_APPS = (
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'mongoTest',
'admin',
)
view.py
db = Connection().sms
...
def login(request):
form = LoginForm()
context_dict = {"form": form}
if request.POST:
form = LoginForm(request.POST)
if form.is_valid():
p = db.user.find_one({
"email": form.cleaned_data["email"],
"password": form.cleaned_data["password"]
})
if p:
request.session['member_id'] = p["_id"]
context_dict["message"] = "Successfully Login"
# request.session.set_test_cookie()
return render(request, "admin/profile.html", context_dict)
context_dict['message'] = "Your username and password didn't match."
return render(request, "admin/login.html", context_dict)
return render(request, "admin/login.html", context_dict)
...
eform.py
class LoginForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.CharField(widget=forms.PasswordInput)
回溯
ConnectionError at /login/
You have not defined a default connection
Request Method: POST
Request URL: http://127.0.0.1:8000/login/
Django Version: 1.6.5
Exception Type: ConnectionError
Exception Value:
You have not defined a default connection
Exception Location: /usr/local/lib/python2.7/dist-packages/mongoengine/connection.py in get_connection, line 93
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/virus/nlocate/project/test-project/djangoMongoTestApp',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/home/virus/nlocate/project/test-project/djangoMongoTestApp/admin/views.py in login
request.session['member_id'] = p["_id"] ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py in __setitem__
self._session[key] = value ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py in _get_session
self._session_cache = self.load() ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/mongoengine/django/sessions.py in load
sess = super(SessionStore, self)._get_session(*args, **kwargs)
if sess.get('_auth_user_id', None):
sess['_auth_user_id'] = str(sess.get('_auth_user_id'))
return sess
def load(self):
try:
s = MongoSession.objects(session_key=self.session_key, ...
expire_date__gt=datetime_now)[0]
if MONGOENGINE_SESSION_DATA_ENCODE:
return self.decode(force_unicode(s.session_data))
else:
return s.session_data
except (IndexError, SuspiciousOperation):
▶ Local vars
/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/manager.py in __get__
queryset = queryset_class(owner, owner._get_collection()) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/mongoengine/document.py in _get_collection
db = cls._get_db() ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/mongoengine/document.py in _get_db
return get_db(cls._meta.get("db_alias", DEFAULT_CONNECTION_NAME)) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/mongoengine/connection.py in get_db
conn = get_connection(alias) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/mongoengine/connection.py in get_connection
raise ConnectionError(msg) ...
▶ Local vars
另外请建议我使用django,mongodb
实现自定义用户身份验证和授权的示例项目答案 0 :(得分:0)
这是因为您在设置文件上设置了SESSION_ENGINE
变量。所以当你请求会话时,它试图连接到mongodb并出现连接错误。
SESSION_ENGINE = 'mongoengine.django.sessions'
当您对上面的块进行注释(使用默认会话引擎)并运行时,连接错误已解决。虽然我不知道mongo会话引擎的默认会话引擎的优势。