Saturday 15 February 2014

Python 2.7 + Django 1.7 + PostgreSQL 9.3: I'm getting a UnicodeEncodeError when trying to save some text to my database. What gives? -



Python 2.7 + Django 1.7 + PostgreSQL 9.3: I'm getting a UnicodeEncodeError when trying to save some text to my database. What gives? -

in models.py file have defined model follows:

class mything(models.model): thing_id = models.charfield(max_length=20, unique=true, null=false) code_snippet = models.textfield(null=true)

i trying populate table bunch of info pulling downwards google api. 1 piece snippet of javascript. 1 of items beingness returned api has snippet of js throwing error.

the error getting is:

unicodeencodeerror: 'ascii' codec can't encode characters in position 213-214: ordinal not in range(128)

the offending bit of text appears in section:

type="text/javascript" charset="utf-\xad\u20108"></script>

i'm \xad, must sort of unicode escape sequence.

i checked postgresql encoding, , using utf-8.

i place line @ top of models.py, didn't create difference:

from __future__ import unicode_literals

what going on here? why can't unicode-aware database save string? also, when did start using 'ascii' codec?

---- edit: total stack trace below -----

error/mainprocess] task apps.r2d_service.tasks.sync_jsnippets_task[e74712bf-2e04-4bed-b08c-f24f9ebb3049] raised unexpected: unicodeencodeerror('ascii', u'<script type="text/javascript">\n window._sm_plcmnt = "finyo_5225_5119";\n var _sm_viewed = false;\n</script>\n<script src="https://cdn.company412media.com/ng/js/augur.js" type="text/javascript" charset="utf-\xad\u20108"></script>\n<script src=\'https://cdn.company412media.com/ng/pub.js\'></script>\n<script type="text/javascript">\n$$(\'#bib_actions_table tr:nth-child(1) a\').each(function (tab) {\n tab.observe(\'click\', function (e, el) {\n if (!_sm_viewed) {\n sm_augur.init();\n _sm_viewed = true;\n }\n });\n});\n\n</script>\n', 213, 215, 'ordinal not in range(128)') traceback (most recent phone call last): file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task r = retval = fun(*args, **kwargs) file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/celery/app/trace.py", line 437, in __protected_call__ homecoming self.run(*args, **kwargs) file "/home/vagrant/myapp/apps/r2d_service/tasks.py", line 25, in sync_jsnippets_task sync_jsnippets() file "/home/vagrant/myapp/apps/r2d_service/sync.py", line 44, in sync_jsnippets myapp_creative.sync(r2d_creative) file "/home/vagrant/myapp/apps/r2d_service/models.py", line 244, in sync self.save() file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 590, in save force_update=force_update, update_fields=update_fields) file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 618, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 680, in _save_table forced_update) file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 724, in _do_update homecoming filtered._update(values) > 0 file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/query.py", line 600, in _update homecoming query.get_compiler(self.db).execute_sql(cursor) file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql cursor = super(sqlupdatecompiler, self).execute_sql(result_type) file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql cursor.execute(sql, params) file "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 90, in execute logger.debug('(%.3f) %s; args=%s' % (duration, sql, params), unicodeencodeerror: 'ascii' codec can't encode characters in position 213-214: ordinal not in range(128)

you need convert strings using base64 can save database.

import base64 base64.b64encode("stings")

python django postgresql unicode utf-8

No comments:

Post a Comment