有趣的情况。数据库错误?蟒蛇。 Django的

时间:2010-08-13 17:48:56

标签: python django

在测试服务器中,它正在运行。但是生产给出了这个追溯: 有什么不同?这个错误意味着什么?

不同之处仅在于python版本。在测试服务器中,它是2.6.5和生产2.5.2。如何在不更改版本的情况下摆脱此错误?

True
2008-10-16 15:20:00
 - did not match our database
Traceback (most recent call last):
  File "./mr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/core/management/base.py", line 195, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/core/management/base.py", line 222, in execute
    output = self.handle(*args, **options)
  File "/usr/local/cluster/dynamic/website/video/remmedia/management/commands/remmedia.py", line 50, in handle
    self.FirstTimeLoad()
  File "/usr/local/cluster/dynamic/website/video/remmedia/management/commands/remmedia.py", line 117, in FirstTimeLoad
    med,created=RemMedia.objects.get_or_create(index=program.Id+50000000, defaults=fields)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/manager.py", line 123, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/query.py", line 335, in get_or_create
    obj.save(force_insert=True)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/base.py", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/base.py", line 495, in save_base
    result = manager._insert(values, return_id=update_pk)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/manager.py", line 177, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/query.py", line 1087, in insert_query
    return query.execute_sql(return_id)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/sql/subqueries.py", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/MySQLdb/cursors.py", line 158, in execute
    query = query % db.literal(args)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/MySQLdb/connections.py", line 265, in literal
    return self.escape(o, self.encoders)
  File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/MySQLdb/connections.py", line 198, in string_literal
    return db.string_literal(obj)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-10: ordinal not in range(128)
经过一段时间的思考和测试。我注意到错误仅在我这样做时显示:

med,created=RemMedia.objects.get_or_create(index=program.Id+50000000, defaults=fields)

经过一段时间的思考结束测试后,我发现了错误。它在字典fields中。我从BeautifulSoup获得了一个变量,代码如下:

                        soup=BeautifulSoup(program.Description.encode('utf-8'))
                        name=soup.find('div',{'class':'head'})
                        fields=dict(
                               name=name.string,
                               description=program.Description.encode('utf-8'),
                               program_name=program.Name.encode('utf-8'),
                               program_date_time=program.RealDateTime,
                               topic_data_time=program.RealDateTime,
                               topic_tag='',
                               created=program.Updated,
                               media=media
                                     )

问题在于name词典中的变量fields

问题仍然存在:如何转换它以免出错?

2 个答案:

答案 0 :(得分:0)

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-10: ordinal not in range(128)

你有一个ascii值>的角色吗? 128?可能是重音字母。

http://www.asciitable.com/

答案 1 :(得分:0)

如果问题出在数据库中,那可能就是一个设置。如果它在python代码中,您可以尝试将此行放在python文件的顶部以处理字符串。

# coding:utf-8