title: UnicodeEncodeError on Django uploaded files
link: http://jj.isgeek.net/2011/06/unicodeencodeerror-on-django-uploaded-files/
author: Jj
description: 
post_id: 738
date: 2011/06/23 14:20:49
created_gmt: 2011/06/23 19:20:49
comment_status: open
slug: unicodeencodeerror-on-django-uploaded-files

This has been an error that's been bugging me for months. For some reason in production settings only in the production server only, whenever a user uploaded a files with a non ascii filename, I'd get this error 
    
    
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 54: ordinal not in range(128)
    

And I was unable to reproduce. 

Looking at the last line of the trace, it would be [os.stat](http://docs.python.org/library/os.html) that would break, and none of my code would be involved, nor the database driver so it seems to be a file system issue. 

The system's locale: 
    
    
    LANG=en_US.utf8
    LC_CTYPE="en_US.utf8"
    LC_NUMERIC="en_US.utf8"
    LC_TIME="en_US.utf8"
    LC_COLLATE="en_US.utf8"
    LC_MONETARY="en_US.utf8"
    LC_MESSAGES="en_US.utf8"
    LC_PAPER="en_US.utf8"
    LC_NAME="en_US.utf8"
    LC_ADDRESS="en_US.utf8"
    LC_TELEPHONE="en_US.utf8"
    LC_MEASUREMENT="en_US.utf8"
    LC_IDENTIFICATION="en_US.utf8"
    LC_ALL=
    

After a lot of wandering around, I ended up on [Django's documentation in the Apache Additional tweaking](https://code.djangoproject.com/wiki/django_apache_and_mod_wsgi#AdditionalTweaking) section, even though my application is running under fastCGI. and I foudn this: 
    
    
    export LANG='en_US.UTF-8'
    export LC_ALL='en_US.UTF-8'
    

It turns out that `LC_ALL` does not have a value. I added those two lines to my fcgi launching script, and problem solved!