Django 应用程序中的 UnicodeEncodeError
我正在尝试在 webfaction 上使用 Django 创建一个应用程序。我基本上是在摆弄亚马逊 API,当其中一个搜索结果具有商标符号时,该符号被传递到我的模板......就会引发错误。我收到错误
渲染时捕获 UnicodeEncodeError: 'ascii' 编解码器无法对位置 9 中的字符 u'\u2122' 进行编码:序数不在范围(128)
并想知道是否有人知道什么解决办法是。
I'm trying to create an app using Django on webfaction. I basically was messing around with the Amazon API, and when one of the search results has a trademark symbol, which is passed to my template...the error is thrown. I'm getting the error
Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'\u2122' in position 9: ordinal not in range(128)
and was wondering if anyone knew what the fix is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能意味着您正在对一段 unicode 数据调用
str()
- 可以调用str
函数ascii
来更好地描述它的作用!您的模板将完全满意 unicode 数据,因此考虑到您正在使用 Django,我怀疑问题出在 __unicode__ 方法或类似方法中。Unicode 是一个棘手的主题,请使用 Google for
python unicode
来感受它。在没有看到更多代码的情况下很难进一步帮助您,但要点是尝试在整个应用程序中使用 unicode 字符串! Python 有一个
unicode()
方法,它的工作方式与处理简单字符串的str
方法完全相同,但也可以很好地处理 unicode 字符串 - 最好使用它。It probably means you are calling
str()
on a a peice of unicode data - thestr
function could be calledascii
to better describe what it does! Your templates will be totally happy with unicode data so given that you are using Django I suspect the problem is in a__unicode__
method or some such.Unicode is a tricky subject, have a Google for
python unicode
to get a feel for it.Tricky to help you further without seeing some more code but the gist is to try and use unicode strings all through your application! Python has a
unicode()
method that works exactly like thestr
method for simple strings but will work fine with unicode strings as well - it's better to use that.是的,
u'\u2122'
是商标符号。在代码中的某个位置,您应该:,或者
需要采取哪些行动以及在哪里?不知道,因为您没有提供回溯...请编辑您的问题以包含完整的回溯,并将其格式化为代码,以便清晰易读。
Yes,
u'\u2122'
is the trade mark sign. Somewhere in your code, you should be:or
Which action is needed and where? No idea, as you haven't supplied a traceback ... please edit your question to include the full traceback, and format it as code, so that it's legible.