1

I get the bellow error, when I request for a Chinese character name image:

line 507, in handle_one_response
    result = self.application(self.environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/middleware.py", line 49, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 156, in __call__
    request = self.request_class(environ)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 80, in __init__
    path_info = get_path_info(environ)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 175, in get_path_info
    path_info = get_bytes_from_wsgi(environ, 'PATH_INFO', '/')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 224, in get_bytes_from_wsgi
    return value.encode(ISO_8859_1) if six.PY3 else value
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 56-58: ordinal not in range(256)

the image url is:

http://localhost:8000/images/qiyun_admin_websitemanage/bannerreconmend/服务器.png

(and the url is exists in my database with the Chinese character)

and I searched the SO, found this post.

it says

db.set_character_set('utf8')
dbc.execute('SET NAMES utf8;')
dbc.execute('SET CHARACTER SET utf8;')
dbc.execute('SET character_set_connection=utf8;')

In my Django project, I can not find the place to add those code.

only the configuration codes there in my settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_mine',
        'USER':'root',
        'PASSWORD':'root',
        'HOST':'127.0.0.1',
        'PORT':'3306',
    }
}

So, how to solve this issue?

user7693832
  • 6,119
  • 19
  • 63
  • 114

1 Answers1

1

As this answer states, the set of valid characters in urls is

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=.`

Other characters should be quoted to be sure that they will be handled correctly. You can quote them using the urllib.parse.quote function.

>>> filename = '服务器.png'
>>> quoted_filename = urllib.parse.quote(filename)
>>> print(quoted_filename)
%E6%9C%8D%E5%8A%A1%E5%99%A8.png

and use the quoted version when constructing your urls.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153