我使用的是带有 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 中将 unicode 字符串压缩在一起 https://stackoverflow.com/questions/10650004/zipping-together-unicode-strings-in-python/10650469#10650469
# -*- 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工程师能够看到这个问题并提供一些帮助。多谢!