I would appreciate your help with a hopefully trivial issue. Recently I’ve started learning Python for work with Google App Engine environment. Needlessly to say I began with the simplest Hello World app.
The English version of it works just fine
However, when I try to work with the signs in my mother tongue, then the problem starts. Basically when run on local machine it does not display letters properly.
Here’s the piece of code that’s causing me issues
# -*- coding: utf-8 -*-
import datetime
print 'Content-Type: text/html'
print ''
print '<html>'
print '<head>'
print '<title>Witaj świecie</title>'
print '</head>'
print '<body>'
print '<h1>Witaj świecie</h1>'
print ''
print 'Data logowania to: %s' % (datetime.datetime.now())
print '</body>'
print '</html>'
Of course I save all files in utf-8 format. Can anyone tell me how to enable proper display of utf-8 characters here?
Set the encoding when you set the content type:
Don’t use a meta tag as @bpgergo suggests; it’s specific to HTML, and simply overrides the headers. Since you’re already setting the headers, it’s easier and better to simply set them correctly in the first place.
You really shouldn’t be using CGI and outputting your content using print statements, though – it’s messy and will be a real pain to maintain as your app gets bigger, as well as making things like internationalization a lot harder. Instead, use WSGI and templating – see the getting started guide for App Engine, specifically this and this.