When I submit a form from react frontend with images server responds "The submitted data was not a file. Check the encoding type on the form.". When I use Django RF browsable API I can successfully add images. Will this be a problem from frontend then ?
VIEWS.PY
class PostListCreateview(generics.ListCreateAPIView):
queryset = Post.objects.all().order_by('id')
serializer_class = PostSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
parser_class = (FileUploadParser,)
def perform_create(self, serializer):
serializer.save(author=self.request.user)
The Post model accepts title,body and pic as parameters from the form POST methods.
REACT FRONTEND
const [image,setImage] = useState(null)
const handlesubmit =()=>{
let endpoint = `/api/post/`;
apiService(endpoint,'POST',{title:title,body:body,pic:image}).then(data=>
console.log(data))};
return (<input type="file" onChange={(e)=>setImage(e.target.files[0])}/>
<Button onClick={handlesubmit} >Upload</Button>)