【问题标题】:disable django rest framework authentication for special functions为特殊功能禁用 django rest 框架身份验证
【发布时间】:2018-08-04 23:35:29
【问题描述】:

我正在使用 django rest 框架进行身份验证。

 'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication'
    ),

但在我的注册函数中:

class UserRegister(APIView):
    @staticmethod
    def post(request, user_name):
.
.
.
.
.

显然我不需要令牌,但是我收到错误:

"detail": "Authentication credentials were not provided."

我尝试了这个可能的答案: this answer

但我遇到了这个错误:

'staticmethod' object has no attribute '__name__'

通过删除@staticmethod 装饰器,我又遇到了之前的错误:

"detail": "Authentication credentials were not provided."

如何排除这个特殊功能需要token?

tnx

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    这是它的选择

    from rest_framework.permissions import AllowAny
    
    class ViewName(APIView):
        permission_classes = [AllowAny]
        def get(self, request, format=None):
            ......
    

    试试这个....

    【讨论】:

      【解决方案2】:

      我认为这可能有效,

      class UserRegister(APIView):
          authentication_classes = [] 
      
          def post(request, user_name):
              # do your stuff
              return Response()
      

      【讨论】:

        【解决方案3】:

        如果您想在任何 DRF 视图上完全禁用身份验证,您可以覆盖 permission_classes 字段。

        您的代码应该类似于:

        class UserRegister(APIView):
        
            permission_classes = []
        
            def post():
        

        【讨论】:

        • 这还能用吗?因为我不断收到 405,所以似乎没有奏效
        猜你喜欢
        • 2013-06-29
        • 2017-09-26
        • 2017-11-02
        • 1970-01-01
        • 2019-03-01
        • 2021-12-21
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        相关资源
        最近更新 更多