Google Text Engine do Google App Engine e UTF-8: quando codificar / decodificar
Estou no Google App Engine 2.5 com Template Django e Webapp Frame.
O db.TextProperty e UTF-8 e Unicode e Decode / Encode me confundiram muito. Eu realmente aprecio alguns especialistas podem oferecer algumas sugestões. Eu pesquisei por toda a noite e ainda tenho muitas perguntas.
O que estou tentando fazer:
[utf-8 form input] => [Python, Store in db.TextProperty] => [When Needed, Replace Japanese with English] => [HTML, UTF-8]
De acordo com esta respostaZipando cadeias unicode em Python
# -*- coding: utf-8 -*-
e todos os arquivos .py salvos no formato utf-8
Aqui está o meu código:
#Model.py
class MyModel(db.Model):
content = db.TextProperty()
#Main.py
def post(self):
content=cgi.escape(self.request.get('content'))
#what is the type of content? Unicode? Str? or Other?
obj = MyModel(content=content)
#obj = MyModel(content=unicode(content))
#obj = MyModel(content=unicode(content,'utf-8'))
#which one is the best?
obj.put()
#Replace one Japanese word with English word in the content
content=obj.content
#what is the type of content here? db.Text? Unicode? Str? or Other?
#content=unicode(obj.content, 'utf-8') #Is this necessary?
content=content.replace(u'ひと',u'hito')
#Output to HTML
self.response.out.write(template.render(path, {'content':content})
#self.response.out.write(template.render(path, {'content':content.encode('utf-8')})
Espero que algum engenheiro do Google App Engine possa ver essa pergunta e oferecer ajuda. Muito obrigado!