Python、Django : ValueError: unsupported format character '(' (0x28) at index 3

标签 python django translation string-formatting

有人可以向我解释为什么这会引发 ValueError 吗? (使用Python 2.7)

from django.utils.translation import ugettext as _

...

template = _('► %(user)s, random text here @[%(friend_id)s] more random text ◄◄◄'.decode('utf-8')) % {'user': friend_profile.user.first_name, 'friend_id': user.id}

这是错误(通过 celery 错误电子邮件发送):

  File "/var/www/myapp/apps/app/tasks.py", line 54, in notify_friends_new_invite
    template=_('��� %(user)s, random text here @[%(friend_id)s] more random text ���������'.decode('utf-8')) % {'user': friend_profile.user.first_name, 'friend_id': user.id},
ValueError: unsupported format character '(' (0x28) at index 3

最佳答案

检查您的消息目录条目。

_(...) 调用将您的 unicode 值替换为消息目录中的值(如果可用),并且消息会抛出此异常。交换 % 和前面的空格就可以做到这一点,例如:

>>> '►% (user)s, random text here @[%(friend_id)s] more random text ◄◄◄'.decode('utf-8') % {'user':u'foo', 'friend_id': u'bar'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unsupported format character '(' (0x28) at index 3

关于Python、Django : ValueError: unsupported format character '(' (0x28) at index 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19448442/

相关文章:

python - 2d 数组的 numpy 掩码,其中所有值都在 1d 数组中

python - int() 和 long() 之间的区别

python - 在 TemplateView 中使用变量

django - 如何在基于函数的 View 而不是类中使用 Django Hitcount?

python - 如何在 django-filter 中创建 MultipleChoiceField?

python - 如何在 Python 中计算多边形的质心

python - 根据另一个列表中的元素从一个列表中删除元素

locale - 什么叫像 "en-US"这样的语言国家前缀?

android - "Social"Android 应用程序中的国际化 - 动态加载资源

angular - 如何在页面初始化时翻译用 ngif 隐藏的文本?