Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

from rest_framework.views import APIView 

from rest_framework import status 

from rest_framework import parsers 

from rest_framework import renderers 

from rest_framework.response import Response 

from rest_framework.authtoken.models import Token 

from rest_framework.authtoken.serializers import AuthTokenSerializer 

 

 

class ObtainAuthToken(APIView): 

    throttle_classes = () 

    permission_classes = () 

    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,) 

    renderer_classes = (renderers.JSONRenderer,) 

    serializer_class = AuthTokenSerializer 

    model = Token 

 

    def post(self, request): 

        serializer = self.serializer_class(data=request.DATA) 

        if serializer.is_valid(): 

            token, created = Token.objects.get_or_create(user=serializer.object['user']) 

            return Response({'token': token.key}) 

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 

 

 

obtain_auth_token = ObtainAuthToken.as_view()