UnicodeEncodeError: 'ascii' codec can't encode character

前端 未结 12 818
予麋鹿
予麋鹿 2020-11-30 20:31

When uploading files with non-ASCII characters I get UnicodeEncodeError:

Exception Type: UnicodeEncodeError at /admin/studio/newsitem/add/
Exception Value: \         


        
12条回答
  •  野性不改
    2020-11-30 20:54

    Just building on answers from this thread and others...

    I had the same issue with genericpath.py giving a UnicodeEncodeError when attempting to upload a file name with non ASCII characters.

    I was using nginx, uwsgi and django with python 2.7.

    Everything was working OK locally but not on the server

    Here are the steps I took 1. added to /etc/nginx/nginx.conf (did not fix the problem)

    http {
        charset utf-8;
    }
    
    1. I added this line to etc/default/locale (did not fix the problem)

      LANGUAGE="en_US.UTF-8"

    2. I followed the instructions here listed under the heading 'Success' https://code.djangoproject.com/wiki/ExpectedTestFailures (did not fix the problem)

      aptitude install language-pack-en-base
      
    3. Found across this ticket https://code.djangoproject.com/ticket/17816 which suggested testing a view on the server to what was happening with locale information

    In your view

    import locale
    locales = "Current locale: %s %s -- Default locale: %s %s" % (locale.getlocale() + locale.getdefaultlocale())
    

    In your template

    {{ locales }}
    

    For me, the issue was that I had no locale and no default locale on my Ubuntu server (though I did have them on my local OSX dev machine) then files with non ASCII file names/paths will not upload correctly with python raising a UnicodeEncodeError, but only on the production server.

    Solution

    I added this to both my site and my site admin uwsgi config files e.g. /etc/uwsgi-emperor/vassals/my-site-config-ini file

    env = LANG=en_US.utf8
    

提交回复
热议问题