How Can I Disable Authentication in Django REST Framework

后端 未结 7 930
庸人自扰
庸人自扰 2020-12-13 23:11

I\'m working on a store site, where every user is going to be anonymous (well, until it\'s time to pay at least), and I\'m trying to use Django REST Framework to serve the p

7条回答
  •  长情又很酷
    2020-12-14 00:09

    If using APIView you can create a permission for the view, example below:

    urls.py

    url(r'^my-endpoint', views.MyEndpoint.as_view())
    

    permissions.py

    class PublicEndpoint(permissions.BasePermission):
        def has_permission(self, request, view):
            return True
    

    views.py

    from permissions import PublicEndpoint
    
    class MyEndpoint(APIView):
    
        permission_classes = (PublicEndpoint,)
    
        def get(self, request, format=None):
            return Response({'Info':'Public Endpoint'})
    

提交回复
热议问题