私は、Django テンプレートと Webapp フレームを使用して Google App Engine 2.5 を使用しています。
db.TextProperty と UTF-8 と Unicode とデコード/エンコードは、私をとても混乱させました。何人かの専門家がいくつかの提案を提供できることを本当に感謝しています. 私は一晩中グーグルで検索しましたが、まだたくさんの質問があります.
私がやろうとしていること:
[utf-8 form input] => [Python, Store in db.TextProperty] => [When Needed, Replace Japanese with English] => [HTML, UTF-8]
この回答によると、Pythonでユニコード文字列を一緒に圧縮する
# -*- coding: utf-8 -*-
および utf-8 形式で保存されたすべての .py ファイル
これが私のコードです:
#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')})
Google App Engine エンジニアがこの質問を見て、助けてくれることを願っています。どうもありがとう!