【发布时间】:2021-07-08 09:01:53
【问题描述】:
当我使用“IsAdminUser”或“IsAuthenticated”时,我无法再访问 api 视图并显示“详细信息”:“未提供身份验证凭据。”
当我将其设置为“AllowAny”或根本不设置身份验证时,它可以正常工作。
Views.py
from rest_framework import generics, status, permissions
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAdminUser
class CreatePost(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminUser,)
parser_classes = [MultiPartParser, FormParser]
def post(self, request, format=None):
print(request.data)
serializer = PostSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Settings.py
INSTALLED_APPS = [
'rest_framework.authtoken',
'rest_framework',
'rest_auth',
'corsheaders',
'django.contrib.sites',
'allauth',
'allauth.account',
'rest_auth.registration',
'allauth.socialaccount',
'news.apps.NewsConfig',
'api.apps.ApiConfig'
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAdminUser',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination'
}
【问题讨论】:
-
您未在请求中提供身份验证信息。包括您正在执行的请求
-
我不明白