• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

219
Views
Django y React: [ErrorDetail(string='Los datos enviados no eran un archivo. Verifique el tipo de codificación en el formulario', code='invalid')]

Recibo el error: [ErrorDetail(string='The submitted data was not a file. Check the encoding type on the form.', code='invalid')] , para enviar una imagen/archivo.

Agregué el tipo de cifrado para el formulario 'multipart/form-data' también en la llamada de Axios, pero sigo teniendo el mismo error.

Tengo un modelo como este:

 class MyModel(models.Model): img = models.ImageField(upload_to='media',blank=False)

En vistas.py:

 class MyModelView(viewset.ModelViewSet): serializer_class = serializer.MyModelSerializer def post(self,request): data = serializer.MyModelSerializer(data=request.data) print(data.is_valid()) # this is false, means there is an error print(data.errors) #This one will display the error shown in the title of this question if data.is_valid(): img = data.data['img'] return Response('Success') return Response('Fail')

En serializador.py:

 class MyModelSerializer(serializers.ModelSerializer): class Meta: model = MyModel fields = '__all__'

En el lado frontal (Reaccionar):

 function ImgComp(){ let [img,setImg] = useState({'image':''} let {image} = img function handleChange(e){ setImg( { ...img,[e.target.name]:e.target.value } ) } function submitForm(e){ e.preventDefault() axios.post('127.0.0.1:8000/mymodelurl/',img,{withCredentials:true,headers:{'Content-Type':'multipart/form-data'}}) return( <div> <form method='post' encType='multipart/form-data' onSubmit = {(e)=>submitForm(e)}> <input type='file' value={image} name='img' onChange={(e)=>handleChange(e)}/> </form> </div> } }

Si depuro el img obtengo algo como:

 C:\\fakepath\\img.png
about 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error