English 中文(简体)
DjangoUnicodeDecodeError and force_unicode
原标题:DjangoUnicodeDecodeError and force_unicode

我提出了简单的Django新闻录入模式:

class NewsEntry(models.Model):
    pub_date = models.DateTimeField( date published )
    title = models.CharField(max_length = 200)
    summary = models.TextField()
    content = models.TextField()

def __unicode__(self):
    return self.title

添加新新闻(Admin page)和“english”文本是有用的,但在试图用白俄罗斯文补充新闻时,有错误:

TemplateSyntaxError at /admin/news/newsenter/

DjangoUnicodeDecodeError, 提供:bcii代码c可按0xd0编码, : 范围不一(128)。 页: 1

Django FCCC/SBI/2008/2。

例外类型:模板

Exception Value: DjangoUnicodeDecodeError, 提供:bcii代码c可按0xd0编码, : 范围不一(128)。 页: 1

特殊地点: /usr/ local/lib/python2.6/dist- Pack/django/utils/encoding.py in >force_unicode, line 88

页: 1

追踪清单上的最后一个项目是:

/usr/ local/lib/python2.6/dist- Packages/django/utils/encoding.py in force_unicode

地方:

e. UnicodeDecodeError(cii , xd0xa2xd0xb5xd1x81x82xd1x80x83x81xd1x81x81d1x81x81x81xd0xbxd0xbexd0xb3xd0xbe , 0, 1, ordinal not in range (128)

准则看上去正确:自称是单编码物体。 此外,djangoproject.com在其/blog application

我花了很多时间解决这一问题,并找到了奇怪的解决办法:

from django.utils.encoding import force_unicode
# ...
def __unicode__(self):
    return force_unicode(self.title)

但是,出于自我考虑。 标题为单编码物体,应无任何改动地重新编号。

为什么return Self. entitled?n t work?

最佳回答
问题回答

通用代码具有丢失数据的潜力。 如果你知道你正在获得哪类数据,那么简单地使用斯图尔德编码方法适当转换数据就更加现实。 这一点可以很容易地以微小的(例如)方式做到:

I_unicode_string = my_latin1_string.decode( latin1 )

我的情况甚至更平静,我从JSON档案中进口数据,创记的事例将 throw成统法协会的编码如下:

DjangoUnicodeDecodeError:  ascii  codec can t decode byte 0xc3 in position 21: ordinal not in range(128). You passed in <Company: [Bad Unicode data]> (<class  companies.models.Company >)

但是,从数据库中检索和操作该代码的工作再次没有问题,因此,如果你出现一个包含<代码>的Django错误,[Bad Unicode data],试图在节省工作费用后再检索该物体。

    ...
company.save()
company = Company.objects.get(pk=company.pk) # avoiding bizarre [Bad Unicode data] error
logger.info("Company (locality exists)  {0}  created".format(company))
...

如果有人想适当解释为什么感到自由,那么我的猜测是输入数据没有在图8中编码:

...
"address_city": "Dolbeau-Mistassini", 
"name": "Bleuets Mistassini Ltu00e9e", 
...




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签