开发者

UnicodeDecode Error at Django Admin (using postgresql) when using special characters in Linux

开发者 https://www.devze.com 2023-03-14 09:31 出处:网络
I\'ve been using a psql/django stack for my application for a while under Windows without any problem at all.

I've been using a psql/django stack for my application for a while under Windows without any problem at all.

However, recently I switched to a linux box and many places started breaking up. Being a spanish speaker my application uses a lot of special characters like "á" or "ñ". Naturally, I decided that my database and default_encoding should be UTF-8 to avoid any funny problems.

Nonetheless, even though the application had no problem displaying objects who had special characters, the admin started having many problems. Whenever an element whose display name had special characters was fetched, I'd get a dreaded:

UnicodeDecodeError at /ajax_select/ajax_lookup/images
'ascii' codec can't decode byte 0xc3 in position 35: or开发者_JAVA百科dinal not in range(128)

Essentially, Django tries to get the unicode() of those elements and fails miserably. I might be missing something, because it has no such problem in my windows box, so, essentially:

  1. PostgreSQL database encoding is UTF-8 in both places (character type and collation are not EXACTLY the same, but I don't think that should make any difference: English_United States.1252 vs. en_US.UTF-8

  2. Apache is not the culprit (nor I don't see how it could be) as this happens with the django development server too.

  3. Django defaults to UTF-8, which is true because as I said, I can see the special characters perfectly in other places of the application, it just fails in a few specific scenarios with the admin.

I've ran out of ideas as to what should be and as the errors lie in the midst of the django admin code it's hard to debug, but mostly it crashes in an str() or unicode() function.

Core of the issue is: In windows str('árbol') = 'árbol'. In Linux str('árbol') crashes.

Any ideas would be really appreciated.


What's the output if you run locale from a command line? I suspect it is not set to UTF-8.


I've solved it horribly by using django's smart_str() every string that may have a weird character and applying a replace filter to that new string so that every character would be replaced by its ascii equivalent. It worked for the most part, yet it made some code look hideous. Ahh, c'est la vie.

0

精彩评论

暂无评论...
验证码 换一张
取 消