Search code examples
djangoencodingwsgi

Django WSGI Python Encoding


I have a strange issue with my Django Apache Wsgi setup. I recently moved my site to a different server. Unfortunately now the encoding is somewhat messed up. When I run a command like this:

barcode.generate_barcode("ean", "1341341234234")

the resulting image will show some special character between every digit, typically for some endocing issue. I guess it is using two bytes instead of one to represent each char or something similar.

enter image description here

If I run the same setup with django ./manage.py runserver comand. The resulting image is fine, no special characters added.

So I came to the conclusion this must be something with my apache2/mod_wsgi setup. But the versions are identical to my old setup, i.e. debian 6, apache2.2, mod_wsgi 3.3.2.

I would like to try a newer version of mod_wsgi but compilation fails.

Can anyone point me in the right direction to where this encoding error might have its cause? To my understanding the WSGI context somehow loads the entire python script with a wrong encoding, otherwise I cannot explain why a hardcoded string would turn out wrong on an image.

In Apache conf.d/charset:

AddDefaultCharset UTF-8

In my vhost additionally:

AddDefaultCharset UTF-8

In apache2/envvars:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

Envvars is used in apache runlevel script:

if [ -z "$APACHE_ENVVARS" ] ; then
    APACHE_ENVVARS=$APACHE_CONFDIR/envvars
fi

I also tried to hardcode overwrite env vars in the runlevel script.

In my wsgi file:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

But nothing seems to help. My image is still generated with broken encoding.


Solution

  • An upgrade of mod_wsgi to version 3.4.0 fixed it.