matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

'utf-8' codec can't decode byte 0xff in position 0: invalid start byte #456

Closed elcolie closed 6 years ago

elcolie commented 6 years ago

Hi. I successfully add my record by DjangoAdmin page. image

Then I would like to test with with my Django REST Framework Problem is as below image

Questions:

  1. Why in the admin page thumbnail does not show the url. Then I can easily click the link
  2. How to fix the error?

Software:

  1. Django 1.11.9
  2. django-imagekit==4.0.2
  3. djangorestframework==3.7.7
elcolie commented 6 years ago

Right now I have to workaround by this. It is not prefect because I have to add my own http://domain.com by myself

import logging

from rest_framework import serializers

from poinkbackend.apps.branches.models import Branch
from poinkbackend.apps.menus.models import Menu

logger = logging.getLogger('django')

class MenuSerializer(serializers.ModelSerializer):
    thumbnail = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Menu
        fields = [
            'id',
            'image',
            'thumbnail',
            'name',
            'description',
            'size',
            'branch',
            'note',
        ]
        read_only_fields = [
            'id',
        ]

    def get_thumbnail(self, menu: Menu):
        return menu.thumbnail.url

    def validate_branch(self, branch: Branch):
        user = self.context['request'].user
        if branch not in Branch.objects.filter(company=user.userprofile.selected_company):
            msg = f'{user} select non-exist branch in {user.userprofile.selected_company}'
            raise serializers.ValidationError(detail=msg)
        return branch
vstoykov commented 6 years ago

The error in /api/menus/ is not related to django-imagekit at all. This looks like utf16 encoding instead of utf8 or something similar.

I didn't get this about http://domain.com. Can you elaborate?

The only problem releated to django-imagekit is probably about your admin. I'm saying probably because I don't know how your definition of the admin looks like. Can you show some code of your admin how you define the thumbnail?

elcolie commented 6 years ago

Here is my admin.py

from django.contrib import admin

from poinkbackend.apps.menus.models import Menu

class MenuAdmin(admin.ModelAdmin):
    __basic_fields = ['id', 'name', 'image', 'thumbnail', 'description', 'size', 'branch']
    list_display = __basic_fields
    search_fields = __basic_fields

admin.site.register(Menu, MenuAdmin)

The problme also occur when I use with django rest image has absolute url thumbnail has not. At here this is want I mean http and domain. I am not sure how to address this properly. I rarely found this issue.

// 20180219225739
// http://localhost:8000/api/menus/1/?format=json

{
  "id": 1,
  "image": "http://localhost:8000/media/uc.png",
  "thumbnail": "/media/CACHE/images/uc/46fbbe0a74051b68a53c0f5536f0af88.jpg",
  "name": "kOzGVMbxqIjEpVvFdNSQwywDHAujbVOECjrQuoBzZmAWUKZoKZ",
  "name_th": "ESwTyAYVuGxvvcUMCcZjyYtFKbgtIQkeqYGDCoNuuHsvsXnhpd",
  "description": "bmnqipAJhbMmukCTebksnQeuAxCiWRPzzUnTlbQtqdVAwHGcfrsFRTTEfTlWyJuodCIQvOAvHrXHNNwMoXvvfXhvlNyJMVNnVivIOqfXyELBAEkHhrJVlUBBICIgwWshgpagvIIaMcJpvuHvNQPLIc",
  "description_th": "DjpnqGOqPSlVGpJOUvvBnlPZyYpgtgJeaPkqjJDWRCVrQgtzaPaImdwZKBLyiYlgjaqhLlgRnPlNHqAFtZZSAndxrFwNIhFKEOdzNoPvvGwXgkfCZXuNXXfbINYUfixRPdYrXrxWESwYCoZmWIfSQs",
  "size": "M",
  "branch": 1,
  "note": {
    "L": 30,
    "M": 20,
    "S": 10,
    "XL": 40
  }
}

FYI. I had followed the docs. But it does not solve my problem. Here is what I did

from django.contrib import admin
from imagekit.admin import AdminThumbnail

from poinkbackend.apps.menus.models import Menu

class MenuAdmin(admin.ModelAdmin):
    admin_thumbnail = AdminThumbnail(image_field='thumbnail')
    __basic_fields = ['id', 'name', 'image', 'thumbnail', 'description', 'size', 'branch']
    list_display = __basic_fields
    search_fields = __basic_fields

admin.site.register(Menu, MenuAdmin)

thumbnail does not show the url when I hover my pointer

image
elcolie commented 6 years ago

Let me open an new issue with simplify example. I really wants to study how to improve django packages so much.

p8ul commented 3 years ago

Try adding this on your settings.py file

AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_QUERYSTRING_AUTH = False
AWS_S3_FILE_OVERWRITE = True