[mod_python] How to set utf-8 charset from mod_python?

Graham Dumpleton grahamd at dscpl.com.au
Thu Feb 1 05:58:26 EST 2007


On 01/02/2007, at 8:25 PM, export at hope.cz wrote:

>
>
>> Like req.write(' <html>....').encode('utf-8')
>>
>> Or
>>
>> Def do_some_processing(Req):
>>  Req.return_html+="<!-- -->\n"
>>
>> Handler(Req):
>>  Req.return_html=""
>>  Req.return_html+="<html>....\n"
>>       do_some_processing(Req)
>>  Req.write(Req.return_html).encode('utf-8')
>>  ......
>>
>>
>> Martijn
>
> Martin,
> Thank you for your reply.
>> req.write(' <html>....').encode('utf-8')
> That  does not work for me. I receive the error:
> AttributeError: 'NoneType' object has no attribute 'encode'
>
> I normally use
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
> in my HTRL page but it does NOT work with mod_python handler for me.
> When I open the page in a browser and check the Encoding from  the  
> browser menu,
> it is set to Western European(ISO) not to UTF-8.
>
> Where can be a problem?

The encode() method is something one calls on strings. If you look up
the Python documentation for string objects you will find what it does.
 From that you might then see that the example he sent simply had some
minor typos (mistakes) in it in that he wrongly applied the encode()  
method
to the result of the write() call and not to the string argument that  
was being
passed as argument to it. Ie., it should be something like:

   body = u"<html> ..... </html>"
   req.write(body.encode('utf-8'))

There is still a bit more to it though as others are pointing out, as  
you have
to be mindful about content of the HTML/XML as well.

Graham


More information about the Mod_python mailing list