import json from django.shortcuts import render from django.shortcuts import get_object_or_404 from rest_framework import viewsets # Create your views here. from django.http import HttpResponse, JsonResponse from rest_framework.parsers import JSONParser from django.contrib.auth.models import User from rest_framework import serializers from rest_framework.response import Response from .serializer import UserProfileSerializer, UserSerializer def user_list(request): ''' List all users or create a user ''' if request.method == 'GET': queryset = User.objects.all() serializer = UserSerializer(queryset, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = UserSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) def user_details(request, pk): if request.method == 'GET': try: queryset = User.objects.get(pk=pk) serializer = UserSerializer(queryset) return JsonResponse(serializer.data, safe=False) except: return JsonResponse({"error":"User does not exist"})