修改权限、用户认证方式
1 2 3 4 5 6 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES' : [ 'rest_framework.authentication.BasicAuthentication' , 'rest_framework.authentication.SessionAuthentication' , ] }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from rest_framework.authentication import SessionAuthentication, BasicAuthenticationfrom rest_framework.permissions import IsAuthenticatedfrom rest_framework.response import Responsefrom rest_framework.views import APIViewclass ExampleView (APIView ): authentication_classes = [SessionAuthentication, BasicAuthentication] permission_classes = [IsAuthenticated] def get (self, request, format =None ): content = { 'user' : unicode(request.user), 'auth' : unicode(request.auth), } return Response(content)
1 2 3 4 5 6 7 8 9 10 11 12 from rest_framework.decorators import permission_classes, authentication_classesclass ExampleView (APIView ): @api_view(['GET' ] ) @authentication_classes([SessionAuthentication, BasicAuthentication] ) @permission_classes([IsAuthenticated] ) def example_view (request, format =None ): content = { 'user' : unicode(request.user), 'auth' : unicode(request.auth), } return Response(content)
1 2 3 4 5 6 7 8 9 10 class ExampleView (APIView ): @action(methods=['get' ], detail=False , url_path='url' , url_name='url' , permission_classes=(IsAuthenticated, ), authentication_classes=(SessionAuthentication, BasicAuthentication ) ) def example_view (request, format =None ): content = { 'user' : unicode(request.user), 'auth' : unicode(request.auth), } return Response(content)
参考链接:django-rest-framework