def countries_detail(request, pk): try: countries = Countries.objects.get(pk=pk) except Countries.DoesNotExist: return JsonResponse({'message': 'The country does not exist'}, status=status.HTTP_400_BAD_REQUEST) if request.method == 'GET': countries_serializer = CountriesSerializer(countries) return JsonResponse(countries_serializer.data) elif request.method == 'PUT': countries_data = JSONParser().parse(request) countries_serializer = CountriesSerializer(countries, data=countries_data) if countries_serializer.is_valid(): countries_serializer.save() return JsonResponse(countries_serializer.data) return JsonResponse(countries_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': countries.delete() return JsonResponse({'message': 'Country was deleted sucessfully'}, status=status.HTTP_400_NO_CONTENT)
def countries_list(request): if request.method == 'GET': countries = Countries.objects.all() name = request.GET.get('name',None) if name is not None: countries = countries.filter(name__icontains=name) countries_serializer = CountriesSerializer(countries,many=True) return JsonResponse(countries_serializer.data,safe=False) elif request.method == 'POST': countries_data = JSONParser().parse(request) countries_serializer = CountriesSerializer(data=countries_data) if countries_serializer.is_valid(): countries_serializer.save() return JsonResponse(countries_serializer.data,status=status.HTTP_201_CREATED) return JsonResponse(countries_serializer.errors, status=status.HTTP_400_BAD_REQUEST)