评论管理员的DjangoUnicodeDecodeError

时间:2014-01-28 20:54:55

标签: mysql django unicode utf-8

我已经通过SQL查询插入:

从PHP CMS将大量阿拉伯语注释导入到此模型中
class Comment(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    author = models.CharField(max_length=60)
    body = models.TextField()
    post = models.ForeignKey(News)

    def __unicode__(self):
        return unicode("%s: %s" % (self.post, self.body[:60]))

插入后,评论在网站上显示正常但在Django的管理页面上我收到此错误:

DjangoUnicodeDecodeError at /admin/news/comment/
'ascii' codec can't decode byte 0xd8 in position 0: ordinal not in range(128). You passed in <Comment: [Bad Unicode data]> (<class 'news.models.Comment'>)

完全引用:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/news/comment/

Django Version: 1.6.1
Python Version: 2.7.3
Installed Applications:
(u'django.contrib.auth',
 u'django.contrib.contenttypes',
 u'django.contrib.sessions',
 u'django.contrib.sites',
 u'django.contrib.messages',
 u'django.contrib.staticfiles',
 u'django.contrib.admin',
 u'news')
Installed Middleware:
(u'django.middleware.common.CommonMiddleware',
 u'django.contrib.sessions.middleware.SessionMiddleware',
 u'django.middleware.csrf.CsrfViewMiddleware',
 u'django.contrib.auth.middleware.AuthenticationMiddleware',
 u'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template /home/qliq/.djenv/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 91
   ascii
   81 :         {% endif %}


   82 :       {% endblock %}


   83 : 


   84 :       <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}


   85 :       {% if cl.formset %}


   86 :         <div>{{ cl.formset.management_form }}</div>


   87 :       {% endif %}


   88 : 


   89 :       {% block result_list %}


   90 :           {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}


   91 :            {% result_list cl %} 


   92 :           {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}


   93 :       {% endblock %}


   94 :       {% block pagination %}{% pagination cl %}{% endblock %}


   95 :       </form>


   96 :     </div>


   97 :   </div>


   98 : {% endblock %}


   99 : 

Traceback:
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/template/base.py" in render
  1196.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  288.             'results': list(results(cl))}
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
  266.             yield ResultList(None, items_for_result(cl, res, None))
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
  258.         super(ResultList, self).__init__(*items)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
  185.             f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/contrib/admin/util.py" in lookup_field
  258.                 value = attr()
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/db/models/base.py" in __str__
  430.             return force_text(self).encode('utf-8')
File "/home/qliq/.djenv/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  116.             raise DjangoUnicodeDecodeError(s, *e.args)

Exception Type: DjangoUnicodeDecodeError at /admin/news/comment/
Exception Value: 'ascii' codec can't decode byte 0xd8 in position 0: ordinal not in range(128). You passed in <Comment: [Bad Unicode data]> (<class 'news.models.Comment'>)

数据库是MySQL 5.5,所有表归类都是utf8_unicode_ci。

这里有什么问题,我该如何解决?

0 个答案:

没有答案