Non Ascii character in upload file crash in python 3

168 views
Skip to first unread message

Luis Zárate

unread,
Jun 8, 2016, 3:44:35 AM6/8/16
to django...@googlegroups.com
Hi, 

I am having this issue in server production, I developed with python 3 and with others fields work great but when file is involved I don't know how to intermediate and remove non ascii characters.
Part of my stack trace is:
Internal Server Error: /vistapublica/perfil/editar

UnicodeEncodeError at /vistapublica/perfil/editar
'ascii' codec can't encode character '\xd1' in position 74: ordinal not in range(128)

FILES: foto = <ExcelInMemoryUploadedFile: ELI. ACUÑA 24 04 15.jpg (image/jpeg)>
So, how can I fix this issue?.



--
"La utopía sirve para caminar" Fernando Birri


Stephen J. Butler

unread,
Jun 8, 2016, 3:58:18 AM6/8/16
to django...@googlegroups.com
Have you tried this?


You probably have a system setup where ASCII is the filesystem encoding. It tells you a way to fix that on Linux/Unix.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG%2B5VyPwsBhF_YSp-Htg_P24x9R2L0nzGgb9bOxsLBRmOr%2BP6w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Luis Zárate

unread,
Jun 8, 2016, 4:02:08 AM6/8/16
to django...@googlegroups.com
$ python manage.py shell

>>> import sys
>>> sys.getfilesystemencoding()
'utf-8'




For more options, visit https://groups.google.com/d/optout.

Stephen J. Butler

unread,
Jun 8, 2016, 4:07:05 AM6/8/16
to django...@googlegroups.com

Luis Zárate

unread,
Jun 8, 2016, 4:07:55 AM6/8/16
to django...@googlegroups.com
I am in Debian,

in /etc/locale.gen
es_CR.UTF-8 UTF-8

 $ locale -a
C
C.UTF-8
es_CR.utf8
POSIX

Luis Zárate

unread,
Jun 8, 2016, 4:09:27 AM6/8/16
to django...@googlegroups.com
After obj.save().

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/db/models/base.py" in save_base
  736.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/db/models/base.py" in _save_table
  798.                       for f in non_pks]

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/db/models/base.py" in <listcomp>
  798.                       for f in non_pks]

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/db/models/fields/files.py" in pre_save
  311.             file.save(file.name, file, save=False)

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/db/models/fields/files.py" in save
  93.             self.name = self.storage.save(name, content, max_length=self.field.max_length)

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/core/files/storage.py" in save
  53.             name = self.get_available_name(name, max_length=max_length)

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/core/files/storage.py" in get_available_name
  89.         while self.exists(name) or (max_length and len(name) > max_length):

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/site-packages/django/core/files/storage.py" in exists
  294.         return os.path.exists(self.path(name))

File "/home/fondomutual/entornos/fomeucr/lib/python3.4/genericpath.py" in exists
  19.         os.stat(path)

Exception Type: UnicodeEncodeError at /vistapublica/perfil/editar
Exception Value: 'ascii' codec can't encode character '\xd1' in position 74: ordinal not in range(128)
Request information:
GET: No GET data



For more options, visit https://groups.google.com/d/optout.

Luis Zárate

unread,
Jun 8, 2016, 4:13:46 AM6/8/16
to django...@googlegroups.com
I am using supervisor and gunicorn for production run as fondomutual user.

$ env | grep LANG
LANG=es_CR.UTF-8
LANGUAGE=es_CR:es

Michal Petrucha

unread,
Jun 8, 2016, 5:28:05 AM6/8/16
to django...@googlegroups.com
On Wed, Jun 08, 2016 at 02:13:23AM -0600, Luis Zárate wrote:
> I am using supervisor and gunicorn for production run as fondomutual user.
>
> $ env | grep LANG
> LANG=es_CR.UTF-8
> LANGUAGE=es_CR:es

Are you certain that when supervisord is started by
init/systemd/upstart, it is also launched with the same locale?
Sometimes, init scripts deliberately set the locale to C or POSIX; I
remember encountering something similar with Apache on Debian a few
years ago.

Good luck,

Michal
signature.asc

Xavier Ordoquy

unread,
Jun 8, 2016, 5:31:20 AM6/8/16
to Django users
Hi,


Le mercredi 8 juin 2016 10:13:46 UTC+2, luisza14 a écrit :
I am using supervisor and gunicorn for production run as fondomutual user.

$ env | grep LANG
LANG=es_CR.UTF-8
LANGUAGE=es_CR:es

Supervisor doesn't propagate the locales to its supervised processes. You need to set them explicitly in process configuration file.

Regards,
Xavier,
Linovia.
Reply all
Reply to author
Forward
0 new messages