Django WSGI Python编码 [英] Django WSGI Python Encoding
问题描述
barcode.generate_barcode(ean,1341341234234)
p>
所得到的图像将在每个数字之间显示一些特殊字符,通常用于某些内容问题。我猜这是使用两个字节代替每个字符或类似的东西。
如果我使用django 运行相同的设置./ manage.py runserver 。所得到的图像很好,没有添加特殊字符。
所以我得出结论,这必须是我的apache2 / mod_wsgi设置的东西。但是这些版本与我的旧设置是一样的,比如debian 6,apache2.2,mod_wsgi 3.3.2。
我想尝试一个较新版本的mod_wsgi,但是编译失败。
任何人都可以指出正确的方向,这个编码错误可能有其原因吗?为了我的理解WSGI上下文以某种方式加载整个python脚本使用错误的编码,否则我无法解释为什么硬编码的字符串将导致错误的图像。
在Apache conf.d / charset:
AddDefaultCharset UTF-8
另外我的vhost:
AddDefaultCharset UTF-8
在apache2 / envvars中:
export LANG ='en_US.UTF-8'
export LC_ALL ='en_US.UTF-8'
Envvars用于apache runlevel脚本:
if [-z$ APACHE_ENVVARS];然后
APACHE_ENVVARS = $ APACHE_CONFDIR / envvars
fi
我还试图硬编码覆盖运行级脚本中的env var。
在我的wsgi文件中:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
但似乎没有任何帮助。我的图像仍然使用破坏的编码生成。
将mod_wsgi升级到版本3.4.0来修复它。
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.
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.
An upgrade of mod_wsgi to version 3.4.0 fixed it.
这篇关于Django WSGI Python编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!