简体   繁体   中英

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. How can I go about it.

This is my function in main.py for uploading the file

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

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

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

from rest_framework import serializers
from .models import File

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

And finally my urls.py

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

With all that, whenever I try to submit the image django outputs unsupported file format. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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