UnicodeEncodeError: 'ascii' codec can't encode character

前端 未结 12 826
予麋鹿
予麋鹿 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 21:05

    As said before, it is related to locale. For exemple, if you use gunicorn to serve your django application, you may have an init.d script (or, as me, a runit script), where you can set the locale.

    To solve UnicodeEncodeError with file upload, put something like export LC_ALL=en_US.UTF8 in your script that run your app.

    For example, this is mine (using gunicorn and runit):

    #!/bin/bash
    export LC_ALL=en_US.UTF8
    cd /path/to/app/projectname
    exec gunicorn_django -b localhost:8000 --workers=2
    

    Also, you can check your locale in your template, using this in your view:

    import locale
    data_to_tpl = {'loc': locale.getlocale(), 'lod_def': locale.getdefaultlocale()}
    

    And just disply {{loc}} - {{loc_def}} in your template.

    You will have more information about your locale settings! That was very usefull for me.

提交回复
热议问题