当我尝试做时,request.user.is_authenticated()
我得到一个ValidationError: None is not a valid ObjectId
我正在尝试追查问题,但我不知道是什么原因造成的。我正在使用 MongoEngine(和 MongoDB。)
我的 settings.py 中有以下内容:
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
'rs.claimutil.auth_backend.ClaimAuthBackend',
)
SESSION_ENGINE = 'mongoengine.django.sessions'
这就是我得到的:
回溯:get_response 111 中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py”。 response = callback(request, *callback_args, **callback_kwargs) 保留 11 中的文件“/Users/bastiano/Documents/ttsf/rsrv/views.py”。如果不是 request.user.is_authenticated():文件“/Library/Frameworks/Python.framework/Versions/2.7/ lib/python2.7/site-packages/django/utils/functional.py”在内部 184.self._setup() 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/django/utils/functional.py" 在 _setup 248. self._wrapped = self._setupfunc() 文件 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/贡献/授权/中间件。py" 在 16. request.user = SimpleLazyObject(lambda: get_user(request)) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/middleware .py" 在 get_user 8. request._cached_user = auth.get_user(request) 文件 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/get_user 101 中的init .py"。用户 = backend.get_user(user_id) 或 AnonymousUser() 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/django/auth .py”在 get_user 149 中。在 with_id 923 中返回 User.objects.with_id(user_id) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” . return self.filter(pk=object_id).first() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py" 在第一个 843. 结果= self [0] getitem中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 1136. return self._document._from_son(self._cursor[key]) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py" in _cursor 579 .self._cursor_obj = self._collection.find(self._query, File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py" in _query 375。 self._mongo_query = self._query_obj.to_query(self._document) 文件 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py" 在 to_query 202. 查询= query.accept(QueryCompilerVisitor(document)) 接受 267 中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py”。在visit_query 159中返回visitor.visit_query(self)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py”。返回QuerySet._transform_query(self.document , **query.query) _transform_query 720 中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py”。 value = field.prepare_query_value(op,值)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/base.py”在prepare_query_value 455。返回self.to_mongo(值)文件“/Library/Frameworks /Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/base.py”在 to_mongo 451.self.error(unicode(e)) 文件中”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/base.py”在错误 203 中。引发 ValidationError(message, errors=errors, field_name=field_name)
异常类型:/rs/claim 处的 ValidationError 异常值:无不是有效的 ObjectId
任何想法为什么会发生这种情况?有没有更简单的方法在 Django + MongoDB 中进行用户身份验证?
视图.py:
def claim(request):
if request.method == 'GET':
if not request.user.is_authenticated():
return shortcuts.redirect('rs/login')
all_b = dbutil.get_b(all=True)
return shortcuts.render_to_response('rs/index.html',
{'all_b':all_b},
context_instance=template.RequestContext(request))
elif request.method == 'POST':
为简单起见,省略了视图的其余部分。我用 ipdb 调试它,if not request.user.is_authenticated()
是问题所在。我之前尝试过使用django.contrib.auth.decorators.login_required.decorator
,但它也失败了。