简体   繁体   中英

How to get input data from django request.POST

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?

Use request.data

name = request.data['name']

This case is only applicable if DRF/Django receives the data from your client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM