简体   繁体   English

如何在kivy中使用rest api将文件上传到django网站

[英]How to use rest api in kivy to upload a file to a django website

I am currently working on a system that involves building a website an a mobile application.我目前正在开发一个涉及构建网站和移动应用程序的系统。 I am using kivy to build my application however am facing an issue with using the django rest api to upload files to the site through kivy.我正在使用 kivy 来构建我的应用程序,但是在使用 django rest api 通过 kivy 将文件上传到站点时遇到了问题。 How can I go about it.我该怎么办。

This is my function in main.py for uploading the file这是我在 main.py 中上传文件的函数

def upload(self, filepass, filename):
    print(str(filename))
    try:
        requests.post('http://127.0.0.1:8000/upload/', filepass)
    except:
        toast('Could not upload file')

This is my api view in my views.py这是我在 views.py 中的 api 视图

class FileUploadView(APIView):
    parser_class = (FileUploadParser,)

    def post(self, request, *args, **kwargs):
      file_serializer = FileSerializer(data=request.data['files'])

      if file_serializer.is_valid():
          file_serializer.save()
          return Response(file_serializer.data, status=status.HTTP_201_CREATED)
      else:
          return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

This is my models.py for the database of the uploaded file这是我上传文件的数据库的models.py

class File(models.Model):
    file = models.ImageField(upload_to='landbroker/', default='default.png')
    def __str__(self):
        return self.file.name

This is my serializers.py for the file upload这是我用于文件上传的 serializers.py

from rest_framework import serializers
from .models import File

class FileSerializer(serializers.ModelSerializer):
    class Meta:
        model = File
        fields = "__all__"

And finally my urls.py最后是我的 urls.py

path('upload/', views.FileUploadView.as_view())

With all that, whenever I try to submit the image django outputs unsupported file format.尽管如此,每当我尝试提交图像时,django 都会输出不受支持的文件格式。 Please help.请帮忙。

I have spent a full year with no answer.我花了整整一年没有答案。 But I now think that if someone just uses get in the django site without verifying the form of the sent media it would actually work.但我现在认为,如果有人只是在 django 站点中使用 get 而不验证发送的媒体的形式,它实际上会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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