带重音符号的Django admin UnicodeEncodeError

时间:2018-08-04 10:45:31

标签: django django-models encoding

每次我将一个带有重音符号的字符添加到Django admin中的条目时,例如“ à”,当我尝试将其删除时,会出现以下错误:

UnicodeEncodeError
'ascii' codec can't encode character u'\xe0' in position 21: ordinal not in range(128)

我在模型中定义了以下方法:

def __unicode__(self):
        return u'%s' %(self.name)

但是它没有任何改变。

然后我尝试返回str(self.id),但是我仍然遇到相同的问题,因此看来问题出在unicode方法上。

有什么主意吗?

编辑:

  • Python 2.7.10
  • Django 1.11.14

追踪

 Traceback (most recent call last):
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
     response = get_response(request)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
     response = self.process_exception_by_middleware(e, request)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
     response = wrapped_callback(request, *callback_args, **callback_kwargs)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 552, in wrapper
     return self.admin_site.admin_view(view)(*args, **kwargs)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
     response = view_func(request, *args, **kwargs)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 224, in inner
     return view(request, *args, **kwargs)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
     return bound_func(*args, **kwargs)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
     return func.__get__(self, type(self))(*args2, **kwargs2)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1713, in delete_view
     return self._delete_view(request, object_id, extra_context)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1737, in _delete_view
     [obj], opts, request.user, self.admin_site, using)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 172, in get_deleted_objects
     to_delete = collector.nested(format_callback)
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 232, in nested
     roots.extend(self._nested(root, seen, format_callback))
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 216, in _nested
     children.extend(self._nested(child, seen, format_callback))
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 218, in _nested
     ret = [format_callback(obj)]
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 145, in format_callback
     force_text(obj))
   File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/encoding.py", line 80, in force_text
     s = six.text_type(bytes(s), encoding, errors)
 UnicodeEncodeError: 'ascii' codec can't encode character u'\\xe0' in position 22: ordinal not in range(128)

0 个答案:

没有答案