【问题标题】:Python: TypeError: 'unicode' object is not callable, conflict with ugettext?Python:TypeError:“unicode”对象不可调用,与 ugettext 冲突?
【发布时间】:2013-03-30 18:27:09
【问题描述】:

我将GeoDjango 示例与class WorldBorder 一起使用。 我的麻烦是我无法打印出所选国家的名称。当我尝试执行时

from django.utils.translation import ugettext_lazy as _
...
location = fromstr(... , srid=4326)
country = WorldBorder.objects.get(mpoly__intersects=location)
print _('User country determined to %s') %country.name

我收到错误消息:

Python: TypeError: 'unicode' object is not callable

当我删除 ugettext_lazy 时,一切正常。 我怎样才能保留翻译选项并使字符串正常工作?

【问题讨论】:

  • 您的错误的完整回溯是什么?在异常点上,print repr(_) 给了你什么?
  • Django 从未抛出完整的回溯,因为所有内容都被except 捕获。删除测试异常显示,未导入 GeoDjango 类。可悲的错误。谢谢你的提示。
  • 对,教训是:不要使用空白的except 子句! :-)

标签: python django unicode typeerror


【解决方案1】:

看起来您正在使用 Python shell。在那里, _ 将取最后一个表达式的值。所以 _ 最终成为第三行之后的 WorldBorder 实例。为避免此问题,当您在 shell 中使用 translate 时,请将 ugettext_lazy 别名为 '_' 以外的其他内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2019-03-02
    • 2015-04-26
    • 2020-10-28
    相关资源
    最近更新 更多