0

I've deployed my Django application recently to pre-set hosting server (Red Hat) and after short usage came to a UnicodeEncode Error after uploaded images, which are containing Cyrillic symbols (russian, to be specific), but saving CharFields data in russian works just fine.

enter image description here

However, the identical code works without a flaw on my personal computers (tested both on Ubuntu and Windows). After file encoding inspection on the server, it turned out, that default system encoding on hosting is ANSI, which is an extension of ASCII

screenshot from SSH client

At this point, I've contacted tech support and they claimed that it may be Django fault because they use UTF-8 encoding on their servers (How then sys.getfilesystemencoding() showcases, that inherited from OS encoding is ASCII?)

My another suspicion were messed locales

enter image description here

And it turned out to be true.

Anyway, is there any way that it's a Django's fault and it can be fixed with Python/Django hacks or is it completely server's fault?

Edit: To be clear, that's not VDS, that's just regular hosting with preset config, so I don't have full control over environment settings, however, I can configure .htaccess file and .fcgi script, so I believe it's Apache server

2 Answers 2

1

I ran into the same issue on my production servers. The important thing is to deploy your django application with the right encoding.

I use gunicorn for deployment and run gunicorn through supervisor. In the supervisor config I defined the encoding:

this is my supervisor config file for the django app:

[program:django_app]
command = /path/to/gunicorn/start/file                 ; Command to start app
user = matyas                                                     ; User to run as
stdout_logfile = /././logs/gunicorn_supervisor.log     ; Where to write log messages
redirect_stderr = true                                            ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8                   ; Set UTF-8 as default encoding

the line

environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8  

is the important declaration. If you don't use supervisor make sure to pass this language argument to the command that starts the django app on your server.

Sign up to request clarification or add additional context in comments.

Comments

1

I've contacted tech support and they claimed (...) they use UTF-8 encoding on their servers

This assertion is debatable at best.

On linux systems, file names encoding is not globally set at the filesystem level but depends on the environment - which is usually set per user but can be overridden before executing a command.

IOW, the effective fs encoding for your django app depends on the environment of the process (apache, gunicorn, whatever) that launches the django processes, and the values you find in your own python shell may not be the one seen by django. You did not mention how you're running your django app so it's impossible atm to tell you exactly where to look but at least the above informations should point you in the right direction.

Anyway, is there any way that it's a Django's fault

Complete bullshit.

and it can be fixed with Python/Django hacks

Definitly not. The proper solution is to fix the process environment, period.

1 Comment

I'm using regular hosting (because with client's budget and needs VDS would be an overkill + said hosting is already paid for 2 years ahead) and it already has pre-set environment for using - the only thing I had to configure was fcgi script and .htaccess file, so I believe it's Apache server. Thank you for the answer, I needed a proof, that it's environment fault and no way Python/Django

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.