gru*_*czy 7 python django unicode gettext
我的django应用程序中有以下代码.
class Status(object):
def __init__(self, id, desc):
self.id = id
self.desc = desc
def __unicode__(self):
return self.desc
STATUS = Status(0, _(u"Some text"))
Run Code Online (Sandbox Code Playgroud)
当我尝试显示某些状态(甚至将其强制转换为unicode)时,我得到:
TypeError: coercing to Unicode: need string or buffer, __proxy__ found
Run Code Online (Sandbox Code Playgroud)
谁能解释我,我做错了什么?
Tho*_*ers 22
_()Django 的函数可以返回一个django.utils.functional.__proxy__对象,它本身不是unicode(参见http://docs.djangoproject.com/en/1.1/ref/unicode/#translated-strings).Python不会unicode()递归调用,因此Status __proxy__对象直接返回对象是错误的.你需要制作__unicode__方法return unicode(self.desc).
请注意,这是Django特有的; Python自己gettext不会返回这些代理对象.
| 归档时间: |
|
| 查看次数: |
6810 次 |
| 最近记录: |