Here I am using django with default authentication. My authentication class in settings.py is
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
}
I set it as a default authentication
but for some API, I don't need Authentication at that time I use allow any but it is not working it required Token like
{
"detail": "Authentication credentials were not provided."
}
My code for POST method is
class EnquiryCrudPost(APIView):
def post(self, request):
UserData = request.data
authentication_classes = (TokenAuthentication)
permission_classes = (AllowAny)
if UserData:
try:
NewUserData = Enquiry.objects.create()
..........
Thank you
CodePudding user response:
The authentication_classes
and permission_classes
should be defined as class attributes, not within your method. Also, it should be list
or tuple
class EnquiryCrudPost(APIView):
authentication_classes = (TokenAuthentication,) # you were missing a comma
permission_classes = (AllowAny,)# you were missing a comma
def post(self, request):
...
In this particular case, the authentication_classes
is not really matter since you wish to use the AllowAny
CodePudding user response:
permission_classes
should be either list or tuple.
authentication_classes = (TokenAuthentication, )
permission_classes = (AllowAny, )