You can write a common function in a different file and import that function in your views.py which will validate for each view whether in request body key is passed or not.
Your common function will look like this
common.py
def get_key(request):
if (key not in request.data):
return Response('please provide key', status=400)
return request.data
And your views will look like this.
views.py
from .common import get_key
@api_view(['GET'])
def userDetail(request,pk):
key = get_key(request) #calling the function to check for key
if key:
users = User.objects.get(id=pk)
serializer = UserSerializer(users, many=False)
return Response(serializer.data)
Here I am assuming you are using a function-based view if you are using class-based view approach will be the same.
Hope it helps. Happy learning.