TAGS :Viewed: 5 - Published at: a few seconds ago

[ UnicodeEncodeError with nginx and django ]

I've tried this SO answer, this doc is inapplicable as I'm running nginx, I've added charset utf-8; to my nginx config and I'm still getting this error.

Summarised traceback is here:

UnicodeEncodeError at /

'ascii' codec can't encode character u'\xe1' in position 69: ordinal not in range(128)


Request Method:     GET
Request URL:    http://django/
Django Version:     1.4.20
Exception Type:     UnicodeEncodeError
Exception Value:    

'ascii' codec can't encode character u'\xe1' in position 69: ordinal not in range(128)

Exception Location:     /opt/envs/venv/lib/python2.7/genericpath.py in getmtime, line 54
Unicode error hint

The string that could not be encoded/decoded was: choacán.jpg

Answer 1


I think this error is not about nginx. It's on the file creation step. Python uses system locale when saving files.

Check your system locale:

 $ python manage.py shell
 > import os
 > print os.popen("locale").read()

If it's incorrect you should set system locale.

But filenames like this can cause any kind of troubles for users. Please think about defining custom file storage for models.FileField and generating random file name for every file - it's good practice.