【问题标题】:How to solve UnicodeDecodeError in mezzanine?如何解决夹层中的 UnicodeDecodeError?
【发布时间】:2013-10-16 06:38:24
【问题描述】:

我正在使用夹层 cms。当我从 blogspot 中删除数据时,我收到了这个错误

blog_id: sanavitastudio
Traceback (most recent call last):
  File "/home/nyros/hs/git_br/2013/Oct-9/healersource/apps/blog_hs/forms.py", line 226, in save
    blog_id=blog_id)
  File "/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 150, in call_command
    return klass.execute(*args, **defaults)
  File "/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/home/nyros/hs/git_br/mezzanine/mezzanine/blog/management/base.py", line 172, in handle
    post, created = BlogPost.objects.get_or_create(**initial)
  File "/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 134, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File "/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 452, in get_or_create
    obj.save(force_insert=True, using=self.db)
  File "/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py", line 221, in save
    super(Displayable, self).save(*args, **kwargs)
  File "/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py", line 77, in save
    super(Slugged, self).save(*args, **kwargs)
  File "/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py", line 46, in save
    super(SiteRelated, self).save(*args, **kwargs)
  File "/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py", line 116, in save
    self.description = strip_tags(self.description_from_content())
  File "/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py", line 146, in description_from_content
    description = unicode(self)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 5: ordinal not in range(128)

代码:

            import xmltest
            blog_id = xmltest.blogname(self.cleaned_data['blog_id'])

            print(type(blog_id))
            call_command(
                'import_blogger_hs',
                mezzanine_user=request.user.username,
                blog_id=blog_id)
            return False

和 blogname 方法如果你给 blogname 它会自动报废 blogID 并表示为 blog_id。

【问题讨论】:

  • 我们可以看看你的代码吗?具体来说:healersource/apps/blog_hs/forms.py ?解决这个问题的最简单方法可能是将所有字符串放在 u'' 字符串中...#python2problems
  • 感谢您的回复。请查看我编辑的代码。

标签: python django mezzanine


【解决方案1】:

啊,所以现在代码问题很可能是request.user.username,不幸的是夹层代码假设它正在接收一个ascii对象(堆栈跟踪中的unicode(self)正在做什么)并且是“双重编码”它.. .grrr!

我会以同样的方式调用你的方法,但这样做:

call_command(
    'import_blogger_hs',
    mezzanine_user=request.user.username.decode('utf-8'),
    blog_id=blog_id)

这能解决问题吗?

【讨论】:

  • 谢谢。正如你所说,我已经更换了我的结束。但是我收到了这个错误 DatabaseError: value too long for type character varying(500).
  • 您可能需要不同的数据库类型来存储您要存储的值?这是来自同一个地方吗?
  • 感谢您的回复。请参阅此链接stackoverflow.com/questions/19263478/…。我发布了另一个关于数据库错误的问题。
  • 嗨@ranman,我发现它与从博客文章中获取数据作为字符串时有关的问题。但它是字符串和 utf-8 或 ascii 的组合。如何解决这个错误?请回复我对 dhana 的评论。
猜你喜欢
  • 2012-02-05
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 2015-06-17
相关资源
最近更新 更多