【发布时间】:2015-01-14 15:32:42
【问题描述】:
我在 django-rest-framework 3.0.3 中定义了一个自定义身份验证方案,如下所示:
from django.contrib.auth.models import AnonymousUser
from rest_framework import authentication
class CustomAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
print 'authenticate!', request.user
return (AnonymousUser(), None)
当我使用CustomAuthentication 时,我得到一个“超出最大递归深度”的错误,这会打印在我的日志中:
authenticate! authenticate! authenticate! authenticate! authenticate! etc.
从我的打印语句中删除request.user 修复了这个无限循环。定义自定义身份验证方案时我不应该使用request.user 吗?
【问题讨论】:
-
您应该使用
request.META.get('field')从请求的标头中检索字段。 -
@cziemba 谢谢。当我做
user = request.META.get('user')时,user是None。
标签: python django django-rest-framework django-1.7