0
votes

I have added JWT authentication in my django react project. what i want to achieve that i want to expose some api without authentication. its a ecommerce project and i want to expose category listing api so that anyone can access without authentication.

settings.py:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':[
    #'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    'rest_framework_simplejwt.authentication.JWTAuthentication',       
    #'rest_framework.authentication.SessionAuthentication',       
   # 'rest_framework.authentication.TokenAuthentication',


],
'DEFAULT_PERMISSION_CLASSES':[
    'rest_framework.permissions.IsAuthenticated',
    #'rest_framework.permissions.AllowAny',

   ],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE':10,



}

views.py:

 @permission_classes((IsAuthenticated,))
 class UserViewSet(viewsets.ModelViewSet):
       queryset = CustomUser.objects.all()
       serializer_class = serializers.UserSerializer


#User Registration View
class CreateUserView(generics.CreateAPIView):
      model = get_user_model()
      permission_classes = [
          permissions.AllowAny 
           ]
      serializer_class = serializers.RegisterSerializer


  #Category Listing View
  @permission_classes((IsAuthenticated,))
  class CategoryView(generics.ListCreateAPIView):
       queryset = Category.objects.all()
       serializer_class = CategorySerializers


 class CategoryDetailView(generics.RetrieveUpdateAPIView):
     queryset = Category.objects.all()
     serializer_class = CategorySerializers

also want to remove pagination from category listing

1

1 Answers

1
votes

You can use decorators to set authentication_class and permission_class to nothing:

from rest_framework.decorators import authentication_classes, permission_classes

class CategoryView(generics.ListCreateAPIView):
   authentication_classes = []
   permission_classes = []

   pagination_class = None
   queryset = Category.objects.all()
   serializer_class = CategorySerializers

And since ListCreateAPIView is an extension of genericViewSet you can set pagination_class