如何在夹层解决UnicodeDecodeError? [英] How to solve UnicodeDecodeError in mezzanine?

查看:168
本文介绍了如何在夹层解决UnicodeDecodeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  blog_id:sanavitastudio 
追溯(最近的呼叫) :
文件/home/nyros/hs/git_br/2013/Oct-9/healersource/apps/blog_hs/forms.py,第226行,保存
blog_id = blog_id)
文件/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/core/management/__init__.py,第150行,call_command
return klass.execute(* args ,** defaults)
文件/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/core/management/base.py,第232行,执行
output = self.handle(* args,** options)
文件/home/nyros/hs/git_br/mezzanine/mezzanine/blog/management/base.py,第172行,处理
post,created = BlogPost.objects.get_or_create(** initial)
文件/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/db/models/manager .py,第134行,get_or_create
return self.get_query_set()。get_or_create(** kwargs)
文件/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/db/models/query.py,第452行,在get_or_create
obj.save(force_insert =真的,使用= self.db)
文件/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py,第221行,保存
super(可显示,自我)。 save(* args,** kwargs)
文件/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py,第77行,保存
super(Slugged,self) .save(* args,** kwargs)
文件/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py,第46行,保存
super(SiteRelated,self ).save(* args,** kwargs)
文件/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py,第116行,保存
self.description = strip_tags(self.description_from_content())
文件/home/nyros/hs/git_br/mezzanine/mezzanine/core/models.py,第146行,描述_from_content
description = unicode(self)
UnicodeDecodeError:'ascii'编解码器不能解码位置5的字节0xe2 :序数不在范围(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

和博客名称方法如果您提供博客名称,它将自动废除blogID,并将其表示为blog_id。

解决方案

啊,所以现在使用代码的问题很可能是 request.user.username 不幸的是夹层代码假设它正在接收一个ascii对象(stacktrace正在做什么 unicode(self)),是double encoding... grrr!



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

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

这是否解决了这个问题?


I am using mezzanine cms. When I scrap the data from the blogspot I got this error

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)

Code:

            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

And blogname method for If you give blogname it will automatically scrap the blogID and it is represented as blog_id.

解决方案

Ah, so now with the code the problem is most likely with request.user.username unfortunately the mezzanine code assumes it is receiving an ascii object (what unicode(self) in the stacktrace is doing) and is "double encoding" it... grrr!

I would call your method the same way but do this:

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

Does that fix the issue?

这篇关于如何在夹层解决UnicodeDecodeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆