0

When I tried to upload a file, my page shows this:

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

os.stat(path)

u'/opt/djangoproject/ConvocatoriaESCA/media_cdn/Luis \xc1ngel Garc\xeda Ramos/Comprobante_Ingl\xe9s_Luis_\xc1ngel_Garc\xeda_Ramos.pdf'

My path to upload Files in models.py have:

def upload_location_comprobante_ingles(instance, filename):
    filename = u"Comprobante_Inglés %s %s.pdf" % (instance.nombre, instance.apellidos)
    return u"%s %s/%s" % (instance.nombre, instance.apellidos, filename)

and nombre and apellidos are UTF-8 strings

Shadow
  • 33,525
  • 10
  • 51
  • 64
Kuroi
  • 63
  • 7
  • You have a problem with your path. Answer is in [this question](https://stackoverflow.com/questions/5974585/python-not-able-to-open-file-with-non-english-characters-in-path#5975944). – Goran Apr 09 '18 at 20:04
  • path.decode('utf8') must be set in setting.py or in the model? – Kuroi Apr 09 '18 at 20:18

1 Answers1

0

The problem is not in os.stat(path)!!

There are two ways how to deploy a django project using Apache.

  1. Basic configuration
  2. Daemon mode

In the both cases mod_wsgi is used.

By default httpd sets environment variable LANG=C. Thus the file system encoding will set to ascii.

You can checking it using sys.getfilesystemencoding(), passed its output to HttpResponse somewhere in your views (or write to a log).

To change LANG environment variable which httpd sets to a mod_wsgi process(es) open file /etc/sysconfig/httpd (on Red Hat based OSes). Then comment LANG=C or change it to your flavour. This approach will work both in Basic configuration and in Daemon mode.

In addition, Daemon mode supports the 'lang' option of the WSGIDaemonProcess command. In this case you will be able to set different a LANG to different virtualhosts.

shmakovpn
  • 751
  • 9
  • 10