I'm getting this error when trying to view objects with names not in english in the django admin console.
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
My .py file's header is
-*- coding: utf-8 -*-
This is the code
class Owner(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
I've looked around for other topics on the subject and found the folowing solutions :
I've tried changing the encoding in the py file to unicode , i've tried writing __str__
as unicode(self.name)
and as u'%s',self.name
. I also tried adding a __unicode__
member with the same combinations. Didn't Work. I've also tried syncing the db again and bouncing the server. Nothing.
The weird thing is that the names are displayed in the menus but when i'm trying to edit the object i'm getting the error.
I would appreciate any help.
Python 2.7, Django 1.5.2, Ubuntu 12 , Postgres.
a Full trace back of the exception
Internal Server Error: /admin/calc/owner/3/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 140, in get_response
response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 82, in rendered_content
content = template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render
return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 124, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 124, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 284, in render
return nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 63, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 284, in render
return nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 84, in render
output = self.filter_expression.resolve(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 606, in resolve
new_obj = func(obj, *arg_vals)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 41, in _dec
args[0] = force_text(args[0])
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 107, in force_text
s = six.text_type(bytes(s), encoding, errors)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
Change your code like this :
class Owner(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
See this link : django unicode error on admin page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With