Opened 10 years ago

Closed 10 years ago

#4834 closed (invalid)

UnicodeEncodeError in admin change_form page

Reported by: perol.chen@… Owned by: Adrian Holovaty
Component: contrib.admin Version: master
Severity: Keywords: UnicodeEncodeError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Malcolm Tredinnick)


when i open the change_form page in admin, i occur the error below:

  Request Method:  	GET[[BR]]

  Request URL: 	/admin/comment/comment/53/ [[BR]]

  Exception Type: 	UnicodeEncodeError[[BR]]

  Exception Value: 	'ascii' codec can't encode characters in position 4-10: ordinal not in range(128)[[BR]]

  Exception Location: 	C:\Python25\lib\site-packages\django\utils\ in force_unicode, line 40[[BR]]

  Python Executable: 	C:\Python25\python.exe[[BR]]

  Python Version: 	2.5.1[[BR]]

C:\Python25\lib\site-packages\django\utils\ in force_unicode
  37. if hasattr(s, '__unicode__'):
  38. s = unicode(s)
  39. else:
'''  40. s = unicode(str(s), encoding, errors) ...             #this is the error line'''
  41. elif not isinstance(s, unicode):
  42. s = unicode(s, encoding, errors)
  43. return s


Change History (5)

comment:1 Changed 10 years ago by Malcolm Tredinnick

Description: modified (diff)

(Fixed description formatting.)

What does the string "s" contain at the point of the error? If you click on the line that says "Local vars", you should be able to see its contents.

comment:2 Changed 10 years ago by perol.chen@…

Local var s's value is : <Judged_object: [56]北京希尔顿酒店-去哪儿旅游搜索引擎>

comment:3 Changed 10 years ago by Malcolm Tredinnick

A couple more things that need checking here. This should work and I can enter and edit non-ASCII data via admin's change form. So I'm trying to work out what is special about your situation (i.e. I cannot repeat the problem at the moment).

What is 'encoding' at the point when the error occurs (again, check "local vars")? Also, can you post the full traceback so I can see where it's coming from? Click on the 'cut-and-paste view' link near the top of the debug page to get something you can paste into Trac (remember to wrap it in {{{ and }}} tags so that Wiki markup does not interfere).

Finally, check that your models __str__ method is explicitly returning UTF-8 encoded data. You cannot just return the value of a field from your model and expect the conversion to happen automatically. The traceback indicates you don't have a __unicode__ method on the object it is trying to convert, which means that the __str__ method is being called and that method must return valid UTF-8 data. However, this might not be a problem, since it is talking about the "ascii" codec, which is why I also need to see the full traceback and the value of 'encoding'.

comment:4 Changed 10 years ago by perol.chen@…

Hi, mtredinnick:

It is good when I change code in my model below:

def __str__(self):
    return self.title


def __unicode__(self):
        return self.title

This is the error traceback before my code changed:

Traceback (most recent call last):
File "C:\Python25\lib\site-packages\django\core\handlers\" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python25\lib\site-packages\django\contrib\admin\views\" in _checklogin
  55. return view_func(request, *args, **kwargs)
File "C:\Python25\lib\site-packages\django\views\decorators\" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File "C:\Python25\lib\site-packages\django\contrib\admin\views\" in change_stage
  322. manipulator = model.ChangeManipulator(object_id)
File "C:\Python25\lib\site-packages\django\db\models\" in __init__
  279. super(AutomaticChangeManipulator, self).__init__(follow=follow)
File "C:\Python25\lib\site-packages\django\db\models\" in __init__
  71. self.fields.extend(f.get_manipulator_fields(self.opts, self, self.change))
File "C:\Python25\lib\site-packages\django\db\models\fields\" in get_manipulator_fields
  245. field_objs, params = self.prepare_field_objs_and_params(manipulator, name_prefix)
File "C:\Python25\lib\site-packages\django\db\models\fields\" in prepare_field_objs_and_params
  515. params['choices'] = self.get_choices_default()
File "C:\Python25\lib\site-packages\django\db\models\fields\" in get_choices_default
  321. return self.get_choices()
File "C:\Python25\lib\site-packages\django\db\models\fields\" in get_choices
  312. lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
File "C:\Python25\lib\site-packages\django\utils\" in smart_unicode
  25. return force_unicode(s, encoding, strings_only, errors)
File "C:\Python25\lib\site-packages\django\utils\" in force_unicode
  40. s = unicode(str(s), encoding, errors)

  UnicodeEncodeError at /admin/comment/comment/53/
  'ascii' codec can't encode characters in position 4-10: ordinal not in range(128)

This is the local vars below:

encoding  	u'utf-8'
errors 	u'strict'
s 	<Judged_object: [56]北京希尔顿酒店-去哪儿旅游搜索引擎>
strings_only 	False

Sorry for my ignoring the change about unicode in svn code and sorry for my poor expression in english

Thanks very much


comment:5 Changed 10 years ago by Malcolm Tredinnick

Resolution: invalid
Status: newclosed

Thanks for all the information. So this isn't a bug in Django. The problem is that your title attribute can be non-ASCII, so encoding matters. You either have to make the change to use the __unicode__ method as you indicate, or else write

def __str__(self):
    return self.title.encode('utf-8')

so that it returns a valid bytestring. What you can't do is return non-UTF-8 data directly from __str__.

Thanks again for providing all the information I asked for so that we could get to the bottom of this. If you're still having trouble porting your code to use the Unicode features, feel free to ask on the django-users list.

Note: See TracTickets for help on using tickets.
Back to Top