Django Basic HTTP Auth“NoneType”对象没有属性“username”

时间:2015-08-17 20:45:56

标签: python django authentication http-basic-authentication django-middleware

这是我的middlware:

<div class="customdiv">
    <div class="col-xs-12.col-sm-6.col-md-4.col-lg-3">
         content here
     </div>
</div>

以下是我们对登录进行的rpc / api调用示例:

 .customdiv{
   boder:2px red;//or something
 }

我在settings.py

中包含了中间件

使用postman尝试api调用时出现此错误:

class BasicAuthMiddleware():

    def process_request(self, request):
        if request.META.get("HTTP_AUTHORIZATION"):
            encoded_auth =  request.META.get("HTTP_AUTHORIZATION")
            encoded_auth = encoded_auth[6:]
            auth = base64.b64decode(encoded_auth)
            auth_email = auth.split(":")[0]
            auth_password = auth.split(":")[1]
            user = authenticate(username=auth_email, password=auth_password)

似乎用户变量等于none。因为通常它是请求的一部分,但在中间件process_request中只能使用2个参数,如@rpc("user", "signin") @pre("email", validate_email) def _(request, user, email, password): user = authenticate(username=email, password=password) if not user: raise ApiException(400, "The email or password you entered is incorrect.") if not user.is_active: raise ApiException(403, "This account has been deactivated.") login(request, user) return user.toDict()

我觉得我在这里不懂东西。我对django很新,所以只想弄明白这一点。在这种情况下,如何使用我的基本http身份验证中间件进行身份验证?

0 个答案:

没有答案