Unfortunately I'm dummy in python... so it's twice harder to find the
reason... But I'm still trying :)

I wrote very simple view:

def main_menu(request):
    html = ''
    menu = Item.root('main').children()
    for s in menu[0].title:
        html = '%s %s' % (html, ord(s))
    html = '%s<br/>%s' % (menu[0].title, html)
    return HttpResponse(html)

And http://test.legalsw.ru/menu/ outputs
АБВ
208 144 208 145 208 146
when my.cnf contains default-character-set = cp1251
and
���
192 193 194
when my.cnf contains default-character-set = utf8

208 144 is 0xd090 which is Cyrillic A code in UTF8
192 is Cyrillic A in CP1251...
"???" is АБВ if browser's character encoding is cp1251

But then if i use python's unicode string, so u'%s<br/>%s' instead of
'%s<br/>%s', I get :
UnicodeDecodeError at /menu/

'ascii' codec can't decode byte 0xc0 in position 0: ordinal not in
range(128)

Request Method:         GET
Request URL:    http://test.legalsw.ru/menu/
Exception Type:         UnicodeDecodeError
Exception Value:

'ascii' codec can't decode byte 0xc0 in position 0: ordinal not in
range(128)

Exception Location:     /home/l/legalswru/test/public_html/menu/views.py
in main_menu, line 11
Python Executable:      /usr/bin/python
Python Version:         2.4.4
Python Path:    ['/home/l/legalswru/test/public_html', '/home/l/
legalswru/test/public_html', '/usr/lib/python24.zip', '/usr/lib/
python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-
tk', '/usr/lib/python2.4/lib-dynload', '/usr/lib/portage/pym', '/usr/
lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/PIL']
Server time:    Чет, 18 Сен 2008 09:17:58 -0500

As I understand python supposes that menu[0].title is ASCII string and
tries to convert it to unicode string... I guess the same happens with
admin templates... Do you have any ideas how to solve this? I've tried
to set use_unicode to True, but nothing changed.


On Sep 18, 3:25 pm, Fedya <[EMAIL PROTECTED]> wrote:
> Still something wrong with character set.
>
> When I use Russian characters inside value of a field in one table,
> Django admin interface can't show list of object corresponding to that
> table. Like I'm trying to accesshttp://test.legalsw.ru/admin/menu/item/
> and getting:
> TemplateSyntaxError at /admin/menu/item/
>
> Caught an exception while rendering: 'ascii' codec can't decode byte
> 0xd0 in position 0: ordinal not in range(128). You passed in
> <django.utils.encoding.DjangoUnicodeDecodeError instance at 0x9ffe06c>
> (<type 'instance'>)
>
> Original Traceback (most recent call last):
>   File "/home/l/legalswru/test/public_html/django/template/debug.py",
> line 71, in render_node
>     result = node.render(context)
>   File "/home/l/legalswru/test/public_html/django/template/
> loader_tags.py", line 24, in render
>     result = self.nodelist.render(context)
>   File "/home/l/legalswru/test/public_html/django/template/
> __init__.py", line 768, in render
>     bits.append(self.render_node(node, context))
>   File "/home/l/legalswru/test/public_html/django/template/debug.py",
> line 78, in render_node
>     wrapped = TemplateSyntaxError(u'Caught an exception while
> rendering: %s' % force_unicode(e, errors='replace'))
>   File "/home/l/legalswru/test/public_html/django/utils/encoding.py",
> line 70, in force_unicode
>     raise DjangoUnicodeDecodeError(s, *e.args)
> DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in
> position 0: ordinal not in range(128). You passed in
> <django.utils.encoding.DjangoUnicodeDecodeError instance at 0x9ffe06c>
> (<type 'instance'>)
>
> Request Method:         GET
> Request URL:    http://test.legalsw.ru/admin/menu/item/
> Exception Type:         TemplateSyntaxError
> Exception Value:
>
> Caught an exception while rendering: 'ascii' codec can't decode byte
> 0xd0 in position 0: ordinal not in range(128). You passed in
> <django.utils.encoding.DjangoUnicodeDecodeError instance at 0x9ffe06c>
> (<type 'instance'>)
>
> Original Traceback (most recent call last):
>   File "/home/l/legalswru/test/public_html/django/template/debug.py",
> line 71, in render_node
>     result = node.render(context)
>   File "/home/l/legalswru/test/public_html/django/template/
> loader_tags.py", line 24, in render
>     result = self.nodelist.render(context)
>   File "/home/l/legalswru/test/public_html/django/template/
> __init__.py", line 768, in render
>     bits.append(self.render_node(node, context))
>   File "/home/l/legalswru/test/public_html/django/template/debug.py",
> line 78, in render_node
>     wrapped = TemplateSyntaxError(u'Caught an exception while
> rendering: %s' % force_unicode(e, errors='replace'))
>   File "/home/l/legalswru/test/public_html/django/utils/encoding.py",
> line 70, in force_unicode
>     raise DjangoUnicodeDecodeError(s, *e.args)
> DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in
> position 0: ordinal not in range(128). You passed in
> <django.utils.encoding.DjangoUnicodeDecodeError instance at 0x9ffe06c>
> (<type 'instance'>)
>
> Exception Location:     /home/l/legalswru/test/public_html/django/
> template/debug.py in render_node, line 81
> Python Executable:      /usr/bin/python
> Python Version:         2.4.4
> Python Path:    ['/home/l/legalswru/test/public_html', '/home/l/
> legalswru/test/public_html', '/usr/lib/python24.zip', '/usr/lib/
> python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-
> tk', '/usr/lib/python2.4/lib-dynload', '/usr/lib/portage/pym', '/usr/
> lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/PIL']
> Server time:    Чет, 18 Сен 2008 07:10:34 -0500
>
> I understand that this is most probably misconfiguration of mysql, but
> I've tried a lot of different configurations using init_command,
> charset, use_unicode and read_default_file parameters... I've also
> tried to reconfigure database to use cp1251 charset, because this is
> default encoding for mysql server, but without positive effect... The
> only difference I can see is a modification of the byte which is 0xd0
> in above error report.
>
> Sorry for my bad English, but I really need any fresh ideas!
>
> On Sep 18, 12:29 pm, Fedya <[EMAIL PROTECTED]> wrote:
>
> > hm... I've tried with very simple model from the scratch and I got the
> > same problem without init_command...
>
> > On Sep 18, 2:30 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
> > wrote:
>
> > > On Thu, 2008-09-18 at 01:57 +0300, Fedor Tyurin wrote:
> > > > Another possible solution (and probably better one) is to use
> > > > init_command option set to 'SET NAMES utf8'.
>
> > > Django and MySQLdb already do this by default. Django passes the
> > > "charset='utf-8'" parameter to the connection and MySQLdb turns this
> > > into a "SET NAMES" call. So you're doing something unusual if this isn't
> > > working by default.
>
> > > Regards,
> > > Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to