1 year ago

#341438

test-img

newbie

unable to access image from request in django

I am trying to send images in FormData() from react to django. But not images is recieving in the django server. Other text fields are showing in QueryDict but images are not there.

react code -

let formdata = new FormData();
formdata.append('title', ref_title.current.value);
formdata.append('price',ref_price.current.value);
formdata.append('description',ref_desc.current.value);
formdata.append('images',ref_image.current.files[0],ref_image.current.files[0].name);
console.log(formdata.get('images'));
axios.post(url, formdata).then(res => {
(res.status == 200)?alert('success'):alert('error');
})

django code -

print(request.POST.get('title'))
print(request.POST.get('price'))
print(request.POST.get('images'))   # it is returning None
print(request.POST)

I am getting the following output in django

hello

41

None

<QueryDict: {'title': ['hello'], 'price': ['41'], 'description': ['hello world']}>

There is no images key in QueryDict. How can I access the image?

reactjs

django

request

payload

0 Answers

Your Answer

Accepted video resources