Hello everyone 
A few days ago I made a school project which consists in doing chat or 
almost mini social network, I use as backend django rest framework because 
it is easy and fast for development, and I use as front-end Kivy client. But 
I encountered a problem that I can't solve it, obviously I searched 
everything on the web but it gives me the same result, here is my code:
models.py:
class Profile(models.Model):
    #user = models.OneToOneField(User)
    name = models.CharField(max_length=20)
    avatar = models.ImageField(upload_to='avatar', blank=True)
    def __str__(self):
        return self.name

serializers.py:
class Base64ImageField(serializers.ImageField):

    def to_internal_value(self, data):
        

        if isinstance(data, six.string_types):
            if 'data:' in data and ';base64,' in data:
                header, data = data.split(';base64,')

            try:
                decoded_file = base64.b64decode(data)
            except TypeError:
                self.fail('invalid_image')

            file_name = str(uuid.uuid4())[:12] # 12 characters are more 
than enough.
            file_extension = self.get_file_extension(file_name, 
decoded_file)
            complete_file_name = "%s.%s" % (file_name, file_extension, )
            data = ContentFile(decoded_file, name=complete_file_name)

        return super(Base64ImageField, self).to_internal_value(data)

    def get_file_extension(self, file_name, decoded_file):
        

        extension = imghdr.what(file_name, decoded_file)
        extension = "jpg" if extension == "jpeg" else extension

        return extension


class ProfileSerializer(serializers.ModelSerializer):
    avatar =  Base64ImageField(max_length=None, use_url=True)

    class Meta:
        model = Profile
        fields = ('id','name','avatar',)

views.py:
class ProfileViewSet(viewsets.ModelViewSet):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer
    parser_classes = (MultiPartParser, JSONParser)

And in my front-end client:
def create_user(self, *args):
        username = self.sm.get_screen('signin_screen').ids.username.text
        password = self.sm.get_screen('signin_screen').ids.sign_pass1.text
        email = self.sm.get_screen('signin_screen').ids.email.text
        image = self.path
        with open(self.path, "rb") as img_file:
            my_string = base64.b64encode(img_file.read())
        print()
        img = my_string.decode('utf-8')

        params = json.dumps({'name': username, 'avatar': img})
        headers = {'Content-type': 'application/json',
                        'Accept': 'application/json'}
        req = UrlRequest('http://127.0.0.1:8000/api/profile/', 
method='POST', on_success=self.user_created, 
on_failure=self.user_login_error, req_body=params,
                            req_headers=headers, 
ca_file=cfi.where(),verify=True, on_error=self.error, debug=True)

If I execute this code it returns me:
Bad Request: /api/profile/
[24/Jul/2021 11:32:49] "POST /api/profile/ HTTP/1.1" 400 418

Please how can resolve this error?

thank you very much 

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/6534011f-5813-485f-9b13-9578a9a0b157n%40googlegroups.com.

Reply via email to