为什么 Django 管理员尝试将字符串编码为 ASCII 而不是 Unicode?或者这个错误与看起来有所不同?

发布于 2024-08-16 09:30:31 字数 497 浏览 3 评论 0原文

我有以下错误:

模板语法错误位于 /admin/results_cop/copsegmentresult/

渲染时捕获异常: ('ascii', 'ISU 欧洲花样滑冰 2009 年锦标赛:成年女子组 女子:短节目 - 2. 苏珊娜 P\xc3\x96YKI\xc3\x96', 98, 99, '序号不在范围(128)')

不会呈现的字符串片段是: PÖYKIÖ

我不明白的是为什么 Django 试图将字符串呈现为 ASCII,为什么不是 UTF-8?

编辑1:

我忘了问 - 我也很想知道如何消除错误;)

编辑2:

Bobince的答案是正确的:)我有一些类似的内容:

def __unicode__(self):
    return "%s %s" (self.foo, self.bar)

I've got the following error:

TemplateSyntaxError at
/admin/results_cop/copsegmentresult/

Caught an exception while rendering:
('ascii', 'ISU European Figure Skating
Championships 2009: Senior Ladies
Ladies: Short Program - 2. Susanna
P\xc3\x96YKI\xc3\x96', 98, 99,
'ordinal not in range(128)')

The fragment of the string that won't render is: PÖYKIÖ

What I don't get is why Django is trying to render the string as ASCII, why not UTF-8?

EDIT 1:

I forgot to ask - I'd also quite like to know how to get rid of the error ;)

EDIT 2:

Bobince's answer is correct :) I had something along the lines of:

def __unicode__(self):
    return "%s %s" (self.foo, self.bar)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂人 2024-08-23 09:30:31

我猜你是在要求 Django 渲染一个字节字符串。开头没有 u

'ISU European Figure Skating Championships 2009: Senior Ladies Ladies: Short Program - 2. Susanna P\xc3\x96YKI\xc3\x96'

因此 Django 可能会尝试将其编码为页面的编码,大概是 UTF-8。但字节串不能直接编码;它们首先必须是 Unicode 字符串。 Python 本身使用默认编码(通常为 ascii)来执行此转换步骤。

>>> 'P\xc3\x96YKI\xc3\x96'.encode('utf-8')
UnicodeDecodeError

因此,您需要做的就是在将字节字符串发送到模板之前,通过 UTF-8 解码自行将该字节字符串转换为 Unicode 字符串。它从哪里来?通常,您应该将应用程序中的所有内容字符串保留为 Unicode 字符串。

I'm guessing you're asking Django to render a byte string. No u at the start of this:

'ISU European Figure Skating Championships 2009: Senior Ladies Ladies: Short Program - 2. Susanna P\xc3\x96YKI\xc3\x96'

So Django is probably trying to encode it to the page's encoding, presumably UTF-8. But byte strings can't be encoded directly; they have to be Unicode strings first. Python itself does this converting step using a default encoding which is typically ascii.

>>> 'P\xc3\x96YKI\xc3\x96'.encode('utf-8')
UnicodeDecodeError

So what you need to do is convert that byte string to a Unicode string yourself by UTF-8-decoding it, before it gets sent to the template. Where has it come from? Usually you should aim to keep all content strings inside your application as Unicode strings.

一念一轮回 2024-08-23 09:30:31

如果有人对 bobince 的答案感到困惑,请想一想:

模型中的字段已经采用 unicode 格式。

当你有这样的 unicode 函数时:

def __unicode__(self):
  return "{0}".format(self.field_one)

它实际上返回一个 ASCII 字符串(这意味着,它将尝试将 field_one 转换为 ASCII),如果 field_one 包含 ASCII 之外的字符,你将遇到上述问题。

现在考虑这个 unicode 函数:

def __unicode__(self):
      return self.field_one

这工作正常,因为您直接返回 unicode 字符串,不需要转换。

让我们回顾一下第一个 unicode 函数,要使其工作,只需添加 u 使其成为 unicode 字符串

def __unicode__(self):
      return u"{0}".format(self.field_one)

If anyone is confused by bobince's answer, think of this:

The fields from the model are already in unicode format.

when you have a unicode function like this:

def __unicode__(self):
  return "{0}".format(self.field_one)

It is actually returning a ASCII string(which means, it will try to convert field_one to ASCII), if the field_one contains characters outside of ASCII, you will get the problem as above.

Now consider this unicode function:

def __unicode__(self):
      return self.field_one

This works fine, because you are returning unicode string directly, no conversion needed.

Lets revisit the first unicode function, to make it work, you just need to add u to make it a unicode string

def __unicode__(self):
      return u"{0}".format(self.field_one)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文