예제 #1
0
파일: views.py 프로젝트: jallgooduw/final2
def device_listAPI(request, format=None):
    if request.method == "GET":
        devices = Device.objects.all()
        serializer = DevicesSerializer(devices, many=True)
        return Response(serializer.data)

    elif request.method == "POST":
        serializer = DevicesSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
파일: views.py 프로젝트: jallgooduw/final2
def device_detailAPI(request, pk, format=None):
    try:
        device = Device.objects.get(pk=pk)
    except Device.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = DevicesSerializer(device)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = DevicesSerializer(device, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.erors, status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        device.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #3
0
def device_listAPI(request, format=None):
    if request.method == "GET":
        devices = Device.objects.all()
        serializer = DevicesSerializer(devices, many=True)
        return Response(serializer.data)

    elif request.method == "POST":
        serializer = DevicesSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #4
0
def device_detailAPI(request, pk, format=None):
    try:
        device = Device.objects.get(pk=pk)
    except Device.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = DevicesSerializer(device)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = DevicesSerializer(device, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.erors, status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        device.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)