GAE中文处理的小例子

      这几个月以来,一直有人发信或者发消息问GAE的中文问题,也怪我,一直没有好好整理出什么文档。在这里简单地写一个吧

以StringProperty和TextProperty这两种类型来举个小例子:

=========================
Model这么定义:
class Gift(db.Model):
name = db.StringProperty(required=True)
description = db.TextProperty()

=========================
在view里这样操作
接收参数时这么写:
description = request.GET.get("memo", None).encode('utf-8')

=========================
保存进datastore时这样写:
      gift.name      = str(name)
      gift.description    = db.Text(description,'utf-8')

=========================
html文件使用utf-8保存。

py文件的开头一行加上 # -*- coding: utf-8 -*-   或#coding=utf-8
py文件也要另存为uft-8格式的文件(否则可能会出现'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128))

如果还有其他什么GAE的问题,可以回此贴,描述一下遇到的问题,我会尽我所能去回答。
原文地址:https://www.cnblogs.com/lexus/p/2061138.html