this is my view.py:
@csrf_exempt
@api_view(['GET', 'POST'])
def create_mobile_view(request):
if request.method == 'GET':
data = Mobile.objects.all()
serializer = MobileSerializer(data, context={'request': request}, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = CreateMobileSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
this is my function based React Component:
const [formData, setformData] = React.useState({
modelName: '',
company: '',
phoneImg1: '',
phoneImg2: '',
phoneImg3: '',
storage: 0,
batteryCapacity: 0,
camera: '',
avilableNumbers: 0,
price: 0,
discountPercentage: 0,
})
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X_CSRFTOKEN': Cookies.get('csrftoken')
},
body: JSON.stringify(formData),
};
function handleChange(event) {
const { name, value } = event.target
setformData(prevInfo => ({
...prevInfo,
[name]: value
}))
};
function submitForm() {
console.log(formData)
fetch('/mobile/create/', requestOptions).then(res => console.log(res))
};