Django admin Chinese character problem.

495 views
Skip to first unread message

Albert Lee

unread,
Jan 10, 2006, 8:57:34 PM1/10/06
to django...@googlegroups.com, python-...@lists.python.cn
I use uft-8 encoding, and in admin page, when I insert a record, the Chinese character will become ?

Gábor Farkas

unread,
Jan 11, 2006, 7:16:28 AM1/11/06
to django...@googlegroups.com, python-...@lists.python.cn
Albert Lee wrote:
> I use uft-8 encoding, and in admin page, when I insert a record, the
> Chinese character will become ?
>

hi,

i am not sure what exactly are you trying to do, here is what i did as a
quick test.


my config:
postgresql-8.0.
database created as UNICODE (which in case of postgres means utf8)
python-2.4
psycopg-1.1.18
django-0.90

i created a very simple application.

started the admin interface,
added text with japanese characters (kanji).
saved it
then opened it again, and it was ok.

i also checked in the database, and the text entered the database correctly.

i created a simple view, and checked the database contents from it.

the text is ok, and it is in non-unicode strings using the utf8
encoding. (so it is NOT in unicode-strings, but utf8-encoded byte-strings).

so a simple view can do for example:

def index(request):
event = events.get_list()[0]
title = event.title.decode('utf8')

//do something with title

bytestring = title.encode('utf8')
return HttpResponse(bytestring)


does this help?

bye,
gabor

Kenneth Gonsalves

unread,
Jan 11, 2006, 8:22:36 AM1/11/06
to django...@googlegroups.com
On Wednesday 11 Jan 2006 5:46 pm, Gábor Farkas wrote:
>         event = events.get_list()[0]
>         title = event.title.decode('utf8')

why do you need this? dont you set the utf8 in the template/webpage
itself?

--
regards
kg

http://www.livejournal.com/users/lawgon
tally ho! http://avsap.org.in
ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

Gábor Farkas

unread,
Jan 11, 2006, 8:48:30 AM1/11/06
to django...@googlegroups.com
Kenneth Gonsalves wrote:
> On Wednesday 11 Jan 2006 5:46 pm, Gábor Farkas wrote:
>
>> event = events.get_list()[0]
>> title = event.title.decode('utf8')
>
>
> why do you need this? dont you set the utf8 in the template/webpage
> itself?
>

?

:)

if i want to work with the title, i HAVE TO decode it, or not?

or is there a way to get unicode data directly from the database-layer?

gabor

Kenneth Gonsalves

unread,
Jan 11, 2006, 9:05:32 AM1/11/06
to django...@googlegroups.com
On Wednesday 11 Jan 2006 7:18 pm, Gábor Farkas wrote:
> Kenneth Gonsalves wrote:
> > On Wednesday 11 Jan 2006 5:46 pm, Gábor Farkas wrote:
> >> event = events.get_list()[0]
> >> title = event.title.decode('utf8')
> >
> > why do you need this? dont you set the utf8 in the
> > template/webpage itself?
>
> ?
>
> :)
>
> if i want to work with the title, i HAVE TO decode it, or not?

not - you are doing all this over the web. If the template/webpage
has the correct setting it will happen automatically

Gábor Farkas

unread,
Jan 11, 2006, 9:15:27 AM1/11/06
to django...@googlegroups.com
Kenneth Gonsalves wrote:
> On Wednesday 11 Jan 2006 7:18 pm, Gábor Farkas wrote:
>
>>Kenneth Gonsalves wrote:
>>
>>>On Wednesday 11 Jan 2006 5:46 pm, Gábor Farkas wrote:
>>>
>>>> event = events.get_list()[0]
>>>> title = event.title.decode('utf8')
>>>
>>>why do you need this? dont you set the utf8 in the
>>>template/webpage itself?
>>
>>?
>>
>>:)
>>
>>if i want to work with the title, i HAVE TO decode it, or not?
>
>
> not - you are doing all this over the web. If the template/webpage
> has the correct setting it will happen automatically

well, maybe there is a misunderstanding.

i am not doing it over the web.

i am loading in data from the database.

please note, that in the part that you quoted, i am only doing:

1. read in data from the database into a bytestring
2. convert the bytestring into an unicode string

these steps have nothing to do with the webpage.

maybe you wanted to quote the other lines where i do the
unicode=>bytestring conversion?


gabor

Jeroen Ruigrok van der Werven

unread,
Jan 11, 2006, 9:28:52 AM1/11/06
to django...@googlegroups.com
For my Japanese<>Dutch dictionary project I didn't have to do anything fancy.

Only thing I had added to my base template is:

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

Nothing else is/was needed.

--
Jeroen Ruigrok van der Werven

Gábor Farkas

unread,
Jan 11, 2006, 9:33:37 AM1/11/06
to django...@googlegroups.com
Jeroen Ruigrok van der Werven wrote:
> For my Japanese<>Dutch dictionary project I didn't have to do anything fancy.
>
> Only thing I had added to my base template is:
>
> <meta http-equiv="content-type" content="text/html;charset=utf-8" />
>
> Nothing else is/was needed.

i understand you. but the code mr. Gonsales quoted did not do anything
with the webpage (at least it seemed to be). it only read something from
the db and that's all.

probably it's a misunderstanding.

about your dictionary project... you never went into unicode? you always
worked with utf8-encoded bytestrings? (just curious)

gabor

Albert Lee

unread,
Jan 11, 2006, 10:38:30 AM1/11/06
to django...@googlegroups.com
I have located the problem on the database: mysql.  and when I change to sqlite, it works pretty
so , I think there should be something character setting matter with the mysql database
( I have made the "set character set =utf8" in mysql shell.)

2006/1/11, Gábor Farkas <ga...@nekomancer.net>:



--
欢迎访问我的小站:   http://www.2tuzi.com
blog :   http://blog.2tuzi.com
Reply all
Reply to author
Forward
0 new messages