Search code examples
djangofile-uploaddjango-adminnginxdjango-uploads

UnicodeEncodeError: 'ascii' codec can't encode character


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

Exception Type: UnicodeEncodeError at /admin/studio/newsitem/add/
Exception Value: 'ascii' codec can't encode character u'\xf8' in position 78: ordinal not in range(128)

See full stack trace.

I run Django 1.2 with MySQL and nginx and FastCGI.

This is a problem that is fixed according to the Django Trac database, but I still have the problem. Any suggestions on how to fix are welcome.

EDIT: This is my image field:

image = models.ImageField(_('image'), upload_to='uploads/images', max_length=100)

Solution

  • After investigating this some more I found out that I hadn't set the charset in my main Nginx config file:

    http {
      charset  utf-8;
    }
    

    By adding the above, the problem disappeared and I think that this is the correct way of handling this issue.