当我尝试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'
这就是我得到的:
回溯:文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py” 在get_response中 111. response = callback(request,* callback_args,** callback_kwargs)文件“/Users/bastiano/Documents/ttsf/rsrv/views.py”保留 11.如果不是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/contrib/auth/middleware.py” 在 16. request.user = SimpleLazyObject(lambda:get_user(request))文件 “/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)File“/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/init py”为 在get_user中 101. user = 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.返回User.objects.with_id(user_id)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 在with_id中 923. return self.filter(pk = object_id).first()File“/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 在第一 843. result = self [0] File“/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 在 getitem 1136. return self._document._from_son(self._cursor [key])文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 在_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” 在_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 = query.accept(QueryCompilerVisitor(document))文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 接受 267. return visitor.visit_query(self)File“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 在visit_query中 159.返回QuerySet._transform_query(self.document,** query.query)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/queryset.py” 在_transform_query中 720.value = field.prepare_query_value(op,value)文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/base.py” 在prepare_query_value中 455. return self.to_mongo(value)文件“/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中进行用户身份验证?
Views.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
,但它也失败了。
答案 0 :(得分:2)
尝试将mongoengine
更新为最新版本。 In master,它是
def get_user(userid):
"""Returns a User object from an id (User.id). Django's equivalent takes
request, but taking an id instead leaves it up to the developer to store
the id in any way they want (session, signed cookie, etc.)
"""
# your installed mongoengine might not include following two lines
if not userid:
return AnonymousUser()
return MongoEngineBackend().get_user(userid) or AnonymousUser()
根据引用,userid
w / None
值会导致问题。
答案 1 :(得分:1)
您使用的是什么版本的MongoEngine / Django?
您是否启用了django.contrib.auth.middleware.AuthenticationMiddleware
?这应该为请求设置User
个实例或AnonymousUser
。
rs.claimutil.auth_backend.ClaimAuthBackend
看起来像什么?它的get_user
方法返回了什么?如果您只有一个身份验证后端,它是否会停止错误?
答案 2 :(得分:0)
在我的一个观点中,我有以下内容:
r_v.obj.backend = 'mongoengine.django.auth.MongoEngineBackend'
,这就是为什么Django忽略了settings.py中的AUTHENTICATION_BACKENDS,并且从未使用过我的自定义身份验证后端。