User34
User34

Reputation: 425

AllowAny for get method in djangorestframework

I use django rest framework, example code

class TestApiView(APIView):

    def get(self, request):
        return Response({'text': 'allow any'})

    def post(self, request):
        return Response({'text': 'IsAuthenticated'})

how to make method GET accessible to all, and method POST only authorized

thank you in advance

Upvotes: 2

Views: 580

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47364

You can use IsAuthenticatedOrReadOnly permission class:

from rest_framework.permissions import IsAuthenticatedOrReadOnly

class TestApiView(APIView):
    permission_classes = (IsAuthenticatedOrReadOnly,)

    def get(self, request):
        return Response({'text': 'allow any'})

    def post(self, request):
        return Response({'text': 'IsAuthenticated'})

Upvotes: 4

Related Questions