Reputation: 1675
I want to get the input data from my frontend so I can create an object from my backend. This is my input request:
return axios.post('http://127.0.0.1:8000/api/products/',{
name: name,
description: description,
category: this.category
},
this.config
)
.then(res=>this.props.update(res.data))
.catch(err=>console.err(err));
where name, description, and category are my input args, and this.config is just the the token.
In my backend I am using a Viewset to handle all my requests. So far I have a one for GET but now I want one for POST. This is my code:
class ProductViewSet(viewsets.ModelViewSet):
def list(self, request):
user = request.user
queryset = Product.objects.filter(user=user)
serializer = ProductSerializer(queryset, many=True)
return Response(serializer.data)
def create(self, request):
data = request.POST["name"]
print("name",name)
return Response("test")
I tried all variations of trying to extract my info from request.POST
I tried request.POST.get('name')
, request.POST['name']
, and other stuff. They all return None/KeyError. What am I doing wrong?
Upvotes: 1
Views: 851
Reputation: 88439
Use request.data
name = request.data['name']
This case is only applicable if DRF/Django receives the data from your client.
Upvotes: 2