有人可以向我解释为什么这会引发 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/