[Answered ]-Name 'serializer' is not defined

1πŸ‘

βœ…

In your post method you use the serializer often, but that variable does not exist, nor did you import something with the name serializer.

Very likely you construct the serializer through the serializer_class, you thus fix this with:

def post(self, request):
    #    ↓  serializer instead of serializer_class
    serializer = self.serializer_class(data=request.data)

    if serializer.is_valid():
        url = serializer.validated_data.get('url')
        message = f'The url is {url}'
        return Response({'message': message})
    else:
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Leave a comment