Martin Matejek wrote:
> Following code returns ISO-8859-1 encoded page, despite explicit uft-8
> declaration:
> @cherrypy.expose
> def index (self):
> cherrypy.response.headers['Content-Type'] = "text/html;
> charset=utf-8"
> return "Moje vznášedlo je plné úhořů [My hovercraft is full of
> eels]"
>
> However, when function use Unicode string, webpage is returned with
> correct, utf-8 charset. Is this bug or feature? I haven't used Python
> for a while so maybe there is some obvious explanation, but I'd like
> to know the "Pythonic" way to serve utf-8 web pages with CherryPy.
The encoding tool will try to convert unicode to str, using the arguments you give it. So you have two options:
1. (Recommended) Emit unicode from all your page handlers, and let the encoding tool encode it. By default, the tool will encode the output as the client prefers it (as declared in the client's "Accept-Charset" request header). The encoding tool will alter the Content-Type response header with the successful charset param.
def index(self):
return u'Moje vzn\xe1\u0161edlo je pln\xe9 \xfaho\u0159\u016f'
1b. Sometimes (especially with HTML forms) you want to force a particular page encoding. Supply tools.encoding.encoding, either in a decorator:
@cherrypy.tools.encoding(encoding='utf-8')
def index(self):
return u'Moje vzn\xe1\u0161edlo je pln\xe9 \xfaho\u0159\u016f'
...or in config:
[/]
tools.encoding.encoding: 'utf-8'
2. Do the encoding yourself, and emit str.
def index(self):
cherrypy.response.headers['Content-Type'] = "text/html; charset=utf-8"
return "Moje vznášedlo je plné úhořů"
Robert Brewer
fuma...@aminus.org