请求中不支持的媒体类型“ application / json; charset = utf-8”。 Django / React文件上传

时间:2019-04-24 07:15:36

标签: django reactjs django-rest-framework

文件上传问题来自对Django的反应

我正在尝试将图像文件从反应上传到Django,但出现以下错误:

请求中不支持的媒体类型“ application / json; charset = utf-8”。

* DJANGO *

class CreateFileProject(viewsets.ModelViewSet):

queryset = Project.objects.all()
serializer_class = ProjectCreateSerializer
parser_classes = (FormParser, MultiPartParser)

def perform_create(self, serializer):
    file_obj = self.request.FILES['file']
    print(file_obj)


class Project(models.Model):

category = models.ForeignKey(Category,on_delete=models.CASCADE)
title = models.CharField(max_length=100,blank=True,null=True)
description=models.CharField(max_length=600,blank=True,null=True)
file=models.ImageField(upload_to='project_mainimage')
videourl=models.CharField(max_length=450,blank=True,null=True)
project_rank=models.IntegerField()
objects=models.Manager()

* REACT *

    this.setState({mainimage:evt.target.files[0]}
    const fd=new FormData();
    fd.append('file',this.state.imgarray[0]);

    console.log("Submitted file ",this.state.imgarray);

   let payload={
        main_image:fd
    };

    axios.post('http://127.0.0.1:8000/portfolio/route/createproj/',payload);

0 个答案:

没有答案