UnicodeDecodeError: ascii codec cannot decode 0xcb byte at position 0: serial number not in range (128) when using tastypie

I am testing tastypie 1.9 with Django 1.4 to create a basic REST API for my site. I follow the initial steps in the documentation where I am stuck.

I am running Django globally and not using virtualenv for this particular implementation. It says in the browser A server error occurred. Please contact the administrator. A server error occurred. Please contact the administrator. . I only run this on the django server.

This is the error message that comes to the terminal when I try to access http://127.0.0.1:8000/api/sessionuserround/?format=json

 [20/Jun/2013 10:26:19] "GET /api/sessionuserround/?format=json HTTP/1.1" 500 99752 Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run self.result = application(self.environ, self.start_response) File "/usr/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__ return self.application(environ, start_response) File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__ response = self.get_response(request) File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 146, in get_response response = debug.technical_404_response(request, e) File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 443, in technical_404_response 'reason': smart_str(exception, errors='replace'), File "/usr/local/lib/python2.7/site-packages/django/utils/encoding.py", line 116, in smart_str return str(s) File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 235, in __repr__ return smart_str(u'<%s %s (%s:%s) %s>' % (self.__class__.__name__, self.urlconf_name, self.app_name, self.namespace, self.regex.pattern)) UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128) [20/Jun/2013 10:26:40] "GET /api/sessionuserround/?format=json HTTP/1.1" 500 59 Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run self.result = application(self.environ, self.start_response) File "/usr/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__ return self.application(environ, start_response) File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__ response = self.get_response(request) File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 146, in get_response response = debug.technical_404_response(request, e) File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 443, in technical_404_response 'reason': smart_str(exception, errors='replace'), File "/usr/local/lib/python2.7/site-packages/django/utils/encoding.py", line 116, in smart_str return str(s) File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 235, in __repr__ return smart_str(u'<%s %s (%s:%s) %s>' % (self.__class__.__name__, self.urlconf_name, self.app_name, self.namespace, self.regex.pattern)) UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128) 

These are my related files:

api.py that exists in sal (my application name):

 from tastypie.resources import ModelResource from sal.models import SessionUserRoundMap class SessionUserRoundResource(ModelResource): class Meta: queryset = SessionUserRoundMap.objects.all() 

Here urls.py:

 from django.conf.urls.defaults import * from sal.api import SessionUserRoundResource sessionuserround_resource = SessionUserRoundResource urlpatterns = patterns('', (r'ˆapi/', include(sessionuserround_resource.urls)), ) 

Relevant code in models.py:

 class SessionRoundMap(models.Model): session_id = models.ForeignKey(Session) num_of_rounds = models.IntegerField() def __unicode(self): text = "Session ID: " + str(self.session_id) return text class SessionUserRoundMap(models.Model): user_id = models.ForeignKey(BssUser) session_id = models.ForeignKey(Session) round_no = models.IntegerField() def __unicode__(self): return self.user_id + ' ' + self.session_id + ' ' + round_no 

Corresponding code in settings.py:

 INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', 'admin', 'tastypie', ) MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', # Uncomment the next line for simple clickjacking protection: # 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) 

My view.py is currently empty.

Here are the requirements of .txt:

 Django==1.4.5 defusedxml==0.4.1 distribute==0.6.40 django-tastypie==0.9.15 dulwich==0.9.0 hg-git==0.4.0 lxml==3.2.1 mercurial==2.6.2 mimeparse==0.1.3 python-dateutil==1.5 python-mimeparse==0.1.4 vboxapi==1.0 virtualenv==1.9.1 wsgiref==0.1.2 

How can I solve this problem? Please, help!

+6
source share
4 answers

The error occurred due to the fact that some models in my models did not return the correct Unicode encoded code when creating the instance.

This was due to a typo in my models.py:

 class SessionRoundMap(models.Model): session_id = models.ForeignKey(Session) num_of_rounds = models.IntegerField() def __unicode(self): text = "Session ID: " + str(self.session_id) return text 

It should be like this:

 class SessionRoundMap(models.Model): session_id = models.ForeignKey(Session) num_of_rounds = models.IntegerField() def __unicode__(self): text = "Session ID: " + str(self.session_id) return text 

The unicode method was spelled incorrectly, which caused this error.

+6
source

I got this error after adding a translation to my site using the ugettext ie utility

 from django.utils.translation import ugettext as _ 

So, anything with non-ascii text will cause these errors, i.e. accents

 messages.success(request, _('Location {0} was deleted.'.format(location_id))) 

Specifying a string is unicode by adding u around it:

 messages.success(request, _(u'Location {0} was deleted.'.format(location_id))) 
+4
source

Somewhere in your database you have an odd entry containing some non ascii code.

Try recording your notes, for example:

 for item in .....all(): print item.name (ect.) 

If the error still occurs, try using this function:

 def strip_non_ascii(string): ''' Returns the string without non ASCII characters''' stripped = (c for c in string if 0 < ord(c) < 127) return ''.join(stripped) 
+2
source

to add to the answers above: Another reason this could happen is to start an HTTP server and request an https connection.

+1
source

Source: https://habr.com/ru/post/947706/


All Articles