Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django Rest框架身份验证_Python_Django_Authentication_Django Rest Framework - Fatal编程技术网

Python Django Rest框架身份验证

Python Django Rest框架身份验证,python,django,authentication,django-rest-framework,Python,Django,Authentication,Django Rest Framework,我正在尝试验证通过web API接口在url中传递的GUID。但是,我无法将GUID传递给我的身份验证类 注意:通过身份验证,我的意思是确保它是一个有效的GUID 我的URL.py: url(r'^customer_address/(?P<guid>[a-z0-9-]+)/(?P<address_id>[a-z0-9-]+)/$', views.CustomerAddressView.as_view()), class CustomerAddressView(

我正在尝试验证通过web API接口在url中传递的GUID。但是,我无法将GUID传递给我的身份验证

注意:通过身份验证,我的意思是确保它是一个有效的GUID

我的URL.py

 url(r'^customer_address/(?P<guid>[a-z0-9-]+)/(?P<address_id>[a-z0-9-]+)/$',
    views.CustomerAddressView.as_view()),
class CustomerAddressView(generics.RetrieveAPIView):
    lookup_field = "address_id"       
    queryset = CustomerAddress.objects.all()
    serializer_class = CustomerAddressSerializer     
REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'customer.authenticate.Authenticate',
        ),
         'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
         )
}
我的设置.py

 url(r'^customer_address/(?P<guid>[a-z0-9-]+)/(?P<address_id>[a-z0-9-]+)/$',
    views.CustomerAddressView.as_view()),
class CustomerAddressView(generics.RetrieveAPIView):
    lookup_field = "address_id"       
    queryset = CustomerAddress.objects.all()
    serializer_class = CustomerAddressSerializer     
REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'customer.authenticate.Authenticate',
        ),
         'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
         )
}
我的客户应用程序中的身份验证类如下所示:

class Authenticate(authentication.BaseAuthentication) :
    def authenticate(self, request):

        request = request._request                               
        guid = getattr(request, 'guid', None)


        my_logger.debug(guid)


        if not guid:
            my_logger.debug('There is no guid!')
            return None


        try:
            user = Customer.objects.get(guid=guid,brand=1)
        except Customer.DoesNotExist:
            raise exceptions.AuthenticationFailed('No such user')

        return None
请求如下所示:

class Authenticate(authentication.BaseAuthentication) :
    def authenticate(self, request):

        request = request._request                               
        guid = getattr(request, 'guid', None)


        my_logger.debug(guid)


        if not guid:
            my_logger.debug('There is no guid!')
            return None


        try:
            user = Customer.objects.get(guid=guid,brand=1)
        except Customer.DoesNotExist:
            raise exceptions.AuthenticationFailed('No such user')

        return None

问题: 我喜欢在类身份验证中检索GUID,并确保其有效。目前,我不断收到您在屏幕截图中看到的错误,我的日志显示:“没有guid!”

如何将guid从请求传递给Authenticate类

谢谢

您可以这样做:

class Authenticate(authentication.BaseAuthentication) :
    def authenticate(self, request):

        request = request._request        

        # This is a bit hacky way to get value of guid                        
        guid = request.path.split('/')[-3]

        my_logger.debug(guid)

        if not guid:
            my_logger.debug('There is no guid!')
        return None

        try:
            user = Customer.objects.get(guid=guid,brand=1)
        except Customer.DoesNotExist:
            raise exceptions.AuthenticationFailed('No such user')

    return None
这有点不正常,因为我试图通过拆分
request.path
about
'/'
来访问
guid
,并访问拆分后获得的列表的最后三个索引

我已经检查过,
self
无法访问我们通常在DRF视图中获得的
kwargs
,因此我们无法访问这里的
kwargs


另一种解决方案是,当通过重写DRF的身份验证过程调用
authenticate()
时,在DRF视图中显式传递
kwargs

检查您是否有权在self中访问
kwargs
。Do
guid=self.kwargs.get('guid',None)
我已经检查过了,您在
authenticate()
中没有DRF视图的
kwargs
。我已经更新了我的ans。另一个解决方案是在调用时将视图的
kwargs
传递到
authenticate()