Hi,
I'm trying to create an app using Django. I'm getting the UnicodeEncodeError, "Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'u2122' in position 9: ordinal not in range(128)", and was wondering if anyone knew what the fix is.
Thanks a lot.
EDIT: Traceback:
Environment:
Request Method: POST
Request URL: http://shopperspoll.webfactional.com/search/results/p=1/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'shopperspoll.polls',
'shopperspoll.search',
'django_facebook',
'django.contrib.admin',
'photologue']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template error:
In template /home/shopperspoll/webapps/django/shopperspoll/mytemplates/search/results.html, error at line 88
Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'\u2122' in position 9: ordinal not in range(128)
78 :
79 : {% if previousPage %}
80 : <div class="page_navigation_previous">
81 : <a href="/search/results/p={{ previousPage }}w={{ keyword }}/"><< Previous Page</a> | 
82 : </div>
83 : {% endif %}
84 :
85 : </div>
86 :
87 : <div class="clear"></div>
88 : {% for i in results %}
89 : <div id="item_number">
90 : {% if forloop.counter0 == 0 %}
91 : {{itemNos.0}}.
92 : {% endif %}
93 : {% if forloop.counter0 == 1 %}
94 : {{itemNos.1}}.
95 : {% endif %}
96 : {% if forloop.counter0 == 2 %}
97 : {{itemNos.2}}.
98 : {% endif %}
Traceback:
File "/home/shopperspoll/webapps/django/lib/python2.7/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/views/decorators/csrf.py" in wrapped_view
39. resp = view_func(*args, **kwargs)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/views/decorators/csrf.py" in wrapped_view
52. return view_func(*args, **kwargs)
File "/home/shopperspoll/webapps/django/shopperspoll/search/views.py" in results
286. return render_to_response('search/results.html', {'userID':userID, 'results':listOfItems, 'totalItems':totalItems, 'totalPages':totalPages, 'pageNo':newPageNo, 'previousPage':previousPage, 'itemNos':itemNos, 'keyword':searchWord}, context)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/template/loader.py" in render_to_string
188. return t.render(context_instance)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/template/base.py" in render
123. return self._render(context)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/shopperspoll/webapps/django/lib/python2.7/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/template/defaulttags.py" in render
227. nodelist.append(node.render(context))
File "/home/shopperspoll/webapps/django/lib/python2.7/django/template/debug.py" in render
92. output = force_unicode(output)
File "/home/shopperspoll/webapps/django/lib/python2.7/django/utils/encoding.py" in force_unicode
74. s = unicode(str(s), encoding, errors)
Exception Type: TemplateSyntaxError at /search/results/p=1/
Exception Value: Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'\u2122' in position 9: ordinal not in range(128)
asked
02 Jun '11, 22:52
iman453
49●18●27
accept rate:
0%
Visiting the URL shows a API error, but that is a GET request. This looks like a POST request, what is getting POSTed? You can use the Django debug toolbar to get more info about the POST and other variables that might be involved, http://docs.webfaction.com/software/django/troubleshooting.html#using-the-django-debug-toolbar
Also another strange thing I see is the '=' in the URL, normally these must be used at the end of a URL to separate query strings,
http://en.wikipedia.org/wiki/Query_string
Is it possible for you to supply some of the urls.py and views.py files without exposing to much info?
Um, basically I'm trying to play around with the Amazon API. When I search for Java, one of the results has the trademark symbol, which is being passed and is causing problems. Django templates can handle unicode by default right? Till the point this error shows up I'm not really storing stuff in my database, so I don't think it should be a database configuration problem.
Django should handle unicode by default, and I personally have used POST requests with Chinese in the past without difficulty.
The best thing to do at this point would be to reduce this error into a very simple test-case and then see if the Django community can provide any insight as to what is causing the issue.