El vie, 24-09-2010 a las 06:30 -0700, Eduardo Robles Elvira escribió:
> Hello everyone,
>
> I'm trying to create a webpy form that when rendered will contain some
> unicode characters like Ñ or áéíóú (I'm spanish). I'm having problem
> with that, and I'm also having trouble when receiving the post data
> from the form, again when the data contains those characters. This is
> some sample code:
>
>
> from __future__ import unicode_literals
> ...
>
> provinces_states = {"Álava" : "País Vasco", ... }
>
> form = web.form.Form(web.form.Textbox('username', name_regexp,
> description=_('Username')),
> form.Dropdown('province',
> [(decode_str(key), decode_str(key)) for key in
> sorted(provinces_states.iterkeys())],
> description=_("Your province")),
> )
>
> Then simply get a view to render that. It fails:
>
> File "/media/home/edulix/proyectos/kurusoft/opengnsys/web/user/
> login.py", line 87, in GET
> return web.ctx.normal_render.user.login(self.form)
> File "/media/home/edulix/proyectos/kurusoft/opengnsys/web/web/
> template.py", line 899, in __call__
> return BaseTemplate.__call__(self, *a, **kw)
> File "/media/home/edulix/proyectos/kurusoft/opengnsys/web/web/
> template.py", line 805, in __call__
> return self.t(*a, **kw)
> File "templates/user/login.html", line 18, in __template__
> </div>
> File "/media/home/edulix/proyectos/kurusoft/opengnsys/web/web/
> form.py", line 44, in render
> out += ' <tr><th><label for="%s">%s</label></th><td>%s</td></tr>
> \n' % (i.id, net.websafe(i.description), html)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
> 151: ordinal not in range(128)
>
>
> Also, if for example the received web.input().username contains
> unicode characters, the same kind of errors. Can someone enlighten me,
> please?
>
I think that your problem is "from __future__ import unicode_literals"
and the classic unicode python problem, I'll explain.
web.form renders the form whit a string operation:
out += ' <tr><th><label for="%s">%s</label></th><td>%s</td></tr>\n' %
(i.id, net.websafe(i.description), html)
That works in python if all strings are unicode, or if all are raw
strings, but doesn't work if you mixed it, net.websafe encode the
string, so the other two should be raw strings too because web.py will
mix it. With from __future__ import uniccode_literals you are defining
all strings as unicode, so i.id should be unicode and html too, that's
the problem.
You can fix that passing arguments to form encoded as utf-8:
form.Dropdown('province'.encode('utf-8'),
[(key.encode('utf-8'), key.encode('utf-8')) for key in
sorted(provinces_states.iterkeys())],
If you remove unicode_literals it's easier, because you only need to
encode strings that you know are unicode, literals strings should be raw
strings without encode.
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.