UnicodeEncodeError in Django Project
Question: Traceback:

    
    
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
      132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
      22.                 return view_func(request, *args, **kwargs)
    File "/home/django/upgrademystartup/project/views.py" in create_or_edit_project
      130.             prj.save()
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
      710.                        force_update=force_update, update_fields=update_fields)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
      738.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _save_table
      800.                       for f in non_pks]
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in pre_save
      315.             file.save(file.name, file, save=False)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in save
      94.             self.name = self.storage.save(name, content, max_length=self.field.max_length)
    File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in save
      54.             name = self.get_available_name(name, max_length=max_length)
    File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in get_available_name
      90.         while self.exists(name) or (max_length and len(name) > max_length):
    File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in exists
      295.         return os.path.exists(self.path(name))
    File "/usr/lib/python2.7/genericpath.py" in exists
      18.         os.stat(path)
    
    Exception Type: UnicodeEncodeError at /project/edit/8
    Exception Value: 'ascii' codec can't encode characters in position 61-66: ordinal not in range(128)
    

wsgi.py:

    
    
    import os
    import sys
    
    from django.core.wsgi import get_wsgi_application
    
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    os.environ['LANG'] = 'en_US.UTF-8'
    os.environ['LC_ALL'] = 'en_US.UTF-8'
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "upgrademystartup.settings")
    
    application = get_wsgi_application()
    

locale page is:

    
    
    getlocale: (None, None)
    getdefaultlocale(): ('en_US', 'UTF-8')
    fs_encoding: ANSI_X3.4-1968
    sys default encoding: utf-8
    

As I'm using gunicorn. Server is **nginx** and configured charset to utf-8.
It's default Django image from DigitalOcean. Language is Russian. I've tried
almost every advice from similar questions.


Answer: It was wrong approach to edit `wsgi.py`, because you need to set **LANG** and
**LC_ALL** variables BEFORE you start Django application.

As for DigitalOcean Django image you should open gunicorn Upstart script
`/etc/gunicorn.d/gunicorn.p` and add two variables just before you start
application:

    
    
    env LANG = en_US.UTF-8
    env LC_AL = en_US.UTF-8
    

