0

I have a url link that contains some Chinese characters( http://localhost:8000/media/imges/qiyun_admin_physicalserver_webmanage/operation_system/域名解析.png ) for request,

if I copy the Chinese contained url from browser address bar, it become this: http://localhost:8000/media/imges/qiyun_admin_physicalserver_webmanage/operation_system/%E5%9F%9F%E5%90%8D%E7%BB%91%E5%AE%9A.png

there gets the error:

'latin-1' codec can't encode characters in position 67-70: ordinal not in range(256)

enter image description here

I found a related post in SO: UnicodeEncodeError: 'latin-1' codec can't encode character

I have checked the database use the utf-8 encode, and the table is utf-8 too:

enter image description here

enter image description here

But why I get the encode error? How to solve this issue?

My backend is Django/Django-Rest-Framework.

sof-03
  • 1,385
  • 2
  • 12
  • 28
  • Something in your code appears to be attempting to encode the URL string in the Latin-1 encoding, which can't handle anything like the full range of Unicode characters. Without sight on any code it would be a guessing game to go further. – holdenweb Apr 04 '18 at 10:34
  • @holdenweb Do you mean the front-end code or backend code? – sof-03 Apr 04 '18 at 10:41
  • You are seeing a Python traceback, so the problem isn't in the front end. – holdenweb Apr 04 '18 at 10:45
  • @holdenweb but how to find the encode Latin-1 code in my backend project. I did nothing. – sof-03 Apr 04 '18 at 11:47

1 Answers1

0

I faced the same problem the problem here is the name of the image "file" I think it is working good at development server and gives error at production server

this needs some configuration to make the server can serve files with names like this. to be in a safe side with urls and file names that will be in urls as mentioned her https://stackoverflow.com/a/1547940/6840261 i came up with solution to change the name of the file or image if it wasn't in proper shape as this answer mention https://stackoverflow.com/a/1547940/6840261 i am
not generating names i only name the file 'a.thesameextension_of_thefile' and django handle multiple files with the same name you have to add code at save() to apply in every change or addition of a new model

class City(models.Model):
    name = models.CharField(max_length=100, verbose_name=_("City Name"),
                            validators=[RegexValidator(r'^[\u0621-\u064A\u0660-\u0669]+$')])
    city_image = models.ImageField(upload_to='city/images/', blank=False, null=True, verbose_name=_("City Image"))

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        name = self.city_image.name
        extension = name.split('.')[-1]
        for char in name:
            if char not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.':
                self.city_image.name = 'a' + '.' + extension
                break
        instance = super(City, self).save(force_insert=False, force_update=False, using=None,
                                          update_fields=None)

this solution will work well , there is another solution as i mentioned in my own question but i think its not a good one as you will have to add code to the core of django request handler and wsgi you can check it How to fix [ERROR] [2699022] wsgiHandler pApp->start_response() return NULL?