Lỗi mysql ascii codec cant encode character năm 2024

Hi,

I am building a fairly large website but I have come a cross one very odd error. We build a simple email record model that has a custom def save() function which when called creates a new Record and fires of the email(s). Now this all works fine till I get a non English PC using this (the error below is from our very basic contact form!)

Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

For some reason when we try to put the email context into the db field the errors out as it trys to ASCII a unicode field.

I already had a lengthy chat with people in IRC who tried to help but we couldn't find what was wrong.

This happens on our live server (debian, apache-mod python 2.4, mysql) and on our development machines (windows, python 2.5, django devserver, mysql 5)

To reproduce it I have to change a virtual machine to some foreign language settings and enter some "odd" chars like ' ` ...

I can "fix" the issue by switching to Postgrsql which seems to not have this problem (tried it and it works fine, but I would need to change some code in other places, so I would prefer to find out what this error is about)?

Google has only a few hits on this with out much use.

Thanks ---

(the whole error is on dpaste.com as well http://dpaste.com/48646/ with code) Traceback:

Environment:

Traceback: File "c:\python25\lib\site-packages\django\core\handlers\base.py" in get_response 82. response = callback(request, *callback_args, **callback_kwargs) File "C:\PROJECTS\OpalStudents\cms\views.py" in ContactUs 60. an_email.save(object) File "C:\PROJECTS\OpalStudents\EmailSystem\models.py" in save 101. super(EmailRecord, self).save() File "c:\python25\lib\site-packages\django\db\models\base.py" in save 272. self.save_base() File "c:\python25\lib\site-packages\django\db\models\base.py" in save_base 329. result = manager._insert(values, return_id=update_pk) File "c:\python25\lib\site-packages\django\db\models\manager.py" in _insert 127. return insert_query(self.model, values, **kwargs) File "c:\python25\lib\site-packages\django\db\models\query.py" in insert_query 729. return query.execute_sql(return_id) File "c:\python25\lib\site-packages\django\db\models\sql \subqueries.py" in execute_sql 300. cursor = super(InsertQuery, self).execute_sql(None) File "c:\python25\lib\site-packages\django\db\models\sql\query.py" in execute_sql 1466. cursor.execute(sql, params) File "c:\python25\lib\site-packages\django\db\backends\util.py" in execute 18. return self.cursor.execute(sql, params) File "c:\python25\lib\site-packages\MySQLdb\cursors.py" in execute 151. query = query % db.literal(args) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in literal 247. return self.escape(o, self.encoders) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in string_literal 180. return db.string_literal(obj)

Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

-124~-~~--~~-~~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-***@googlegroups.com To unsubscribe from this group, send email to django-users-***@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~~-

as an extra note, all Tables and Fields involved in this are set to Utf8 / unicode.

Hi, I am building a fairly large website but I have come a cross one very odd error. We build a simple email record model that has a custom def save() function which when called creates a new Record and fires of the email(s). Now this all works fine till I get a non English PC using this (the error below is from our very basic contact form!) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128) For some reason when we try to put the email context into the db field the errors out as it trys to ASCII a unicode field. I already had a lengthy chat with people in IRC who tried to help but we couldn't find what was wrong. This happens on our live server (debian, apache-mod python 2.4, mysql) and on our development machines (windows, python 2.5, django devserver, mysql 5) To reproduce it I have to change a virtual machine to some foreign language settings and enter some "odd" chars like ' ` ... I can "fix" the issue by switching to Postgrsql which seems to not have this problem (tried it and it works fine, but I would need to change some code in other places, so I would prefer to find out what this error is about)? Google has only a few hits on this with out much use. Thanks --- (the whole error is on dpaste.com as wellhttp://dpaste.com/48646/ with code) File "c:\python25\lib\site-packages\django\core\handlers\base.py" in get_response 82. response = callback(request, *callback_args, **callback_kwargs) File "C:\PROJECTS\OpalStudents\cms\views.py" in ContactUs 60. an_email.save(object) File "C:\PROJECTS\OpalStudents\EmailSystem\models.py" in save 101. super(EmailRecord, self).save() File "c:\python25\lib\site-packages\django\db\models\base.py" in save 272. self.save_base() File "c:\python25\lib\site-packages\django\db\models\base.py" in save_base 329. result = manager._insert(values, return_id=update_pk) File "c:\python25\lib\site-packages\django\db\models\manager.py" in _insert 127. return insert_query(self.model, values, **kwargs) File "c:\python25\lib\site-packages\django\db\models\query.py" in insert_query 729. return query.execute_sql(return_id) File "c:\python25\lib\site-packages\django\db\models\sql \subqueries.py" in execute_sql 300. cursor = super(InsertQuery, self).execute_sql(None) File "c:\python25\lib\site-packages\django\db\models\sql\query.py" in execute_sql 1466. cursor.execute(sql, params) File "c:\python25\lib\site-packages\django\db\backends\util.py" in execute 18. return self.cursor.execute(sql, params) File "c:\python25\lib\site-packages\MySQLdb\cursors.py" in execute 151. query = query % db.literal(args) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in literal 247. return self.escape(o, self.encoders) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in string_literal 180. return db.string_literal(obj) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

-272~-~~--~~-~~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-***@googlegroups.com To unsubscribe from this group, send email to django-users-***@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~~-

Hi,

The strings you are saving into database may need to be encoded to UTF-8 first, e.g: "A string".encode('utf-8')

Julien

as an extra note, all Tables and Fields involved in this are set to Utf8 / unicode.

Hi, I am building a fairly large website but I have come a cross one very odd error. We build a simple email record model that has a custom def save() function which when called creates a new Record and fires of the email(s). Now this all works fine till I get a non English PC using this (the error below is from our very basic contact form!) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128) For some reason when we try to put the email context into the db field the errors out as it trys to ASCII a unicode field. I already had a lengthy chat with people in IRC who tried to help but we couldn't find what was wrong. This happens on our live server (debian, apache-mod python 2.4, mysql) and on our development machines (windows, python 2.5, django devserver, mysql 5) To reproduce it I have to change a virtual machine to some foreign language settings and enter some "odd" chars like ' ` ... I can "fix" the issue by switching to Postgrsql which seems to not have this problem (tried it and it works fine, but I would need to change some code in other places, so I would prefer to find out what this error is about)? Google has only a few hits on this with out much use. Thanks --- (the whole error is on dpaste.com as wellhttp://dpaste.com/48646/ with code) File "c:\python25\lib\site-packages\django\core\handlers\base.py" in get_response 82. response = callback(request, *callback_args, **callback_kwargs) File "C:\PROJECTS\OpalStudents\cms\views.py" in ContactUs 60. an_email.save(object) File "C:\PROJECTS\OpalStudents\EmailSystem\models.py" in save 101. super(EmailRecord, self).save() File "c:\python25\lib\site-packages\django\db\models\base.py" in save 272. self.save_base() File "c:\python25\lib\site-packages\django\db\models\base.py" in save_base 329. result = manager._insert(values, return_id=update_pk) File "c:\python25\lib\site-packages\django\db\models\manager.py" in _insert 127. return insert_query(self.model, values, **kwargs) File "c:\python25\lib\site-packages\django\db\models\query.py" in insert_query 729. return query.execute_sql(return_id) File "c:\python25\lib\site-packages\django\db\models\sql \subqueries.py" in execute_sql 300. cursor = super(InsertQuery, self).execute_sql(None) File "c:\python25\lib\site-packages\django\db\models\sql\query.py" in execute_sql 1466. cursor.execute(sql, params) File "c:\python25\lib\site-packages\django\db\backends\util.py" in execute 18. return self.cursor.execute(sql, params) File "c:\python25\lib\site-packages\MySQLdb\cursors.py" in execute 151. query = query % db.literal(args) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in literal 247. return self.escape(o, self.encoders) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in string_literal 180. return db.string_literal(obj) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

-420~-~~--~~-~~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-***@googlegroups.com To unsubscribe from this group, send email to django-users-***@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~~-

Hi Julien,

thanks for that! it solved it! but I still don't quite get why this would trigger the error. As all the data should already be in unicode?

thanks again!

o

Hi, The strings you are saving into database may need to be encoded to UTF-8 first, e.g: "A string".encode('utf-8') Julien

as an extra note, all Tables and Fields involved in this are set to Utf8 / unicode.

Hi, I am building a fairly large website but I have come a cross one very odd error. We build a simple email record model that has a custom def save() function which when called creates a new Record and fires of the email(s). Now this all works fine till I get a non English PC using this (the error below is from our very basic contact form!) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128) For some reason when we try to put the email context into the db field the errors out as it trys to ASCII a unicode field. I already had a lengthy chat with people in IRC who tried to help but we couldn't find what was wrong. This happens on our live server (debian, apache-mod python 2.4, mysql) and on our development machines (windows, python 2.5, django devserver, mysql 5) To reproduce it I have to change a virtual machine to some foreign language settings and enter some "odd" chars like ' ` ... I can "fix" the issue by switching to Postgrsql which seems to not have this problem (tried it and it works fine, but I would need to change some code in other places, so I would prefer to find out what this error is about)? Google has only a few hits on this with out much use. Thanks --- (the whole error is on dpaste.com as wellhttp://dpaste.com/48646/ with code) File "c:\python25\lib\site-packages\django\core\handlers\base.py" in get_response 82. response = callback(request, *callback_args, **callback_kwargs) File "C:\PROJECTS\OpalStudents\cms\views.py" in ContactUs 60. an_email.save(object) File "C:\PROJECTS\OpalStudents\EmailSystem\models.py" in save 101. super(EmailRecord, self).save() File "c:\python25\lib\site-packages\django\db\models\base.py" in save 272. self.save_base() File "c:\python25\lib\site-packages\django\db\models\base.py" in save_base 329. result = manager._insert(values, return_id=update_pk) File "c:\python25\lib\site-packages\django\db\models\manager.py" in _insert 127. return insert_query(self.model, values, **kwargs) File "c:\python25\lib\site-packages\django\db\models\query.py" in insert_query 729. return query.execute_sql(return_id) File "c:\python25\lib\site-packages\django\db\models\sql \subqueries.py" in execute_sql 300. cursor = super(InsertQuery, self).execute_sql(None) File "c:\python25\lib\site-packages\django\db\models\sql\query.py" in execute_sql 1466. cursor.execute(sql, params) File "c:\python25\lib\site-packages\django\db\backends\util.py" in execute 18. return self.cursor.execute(sql, params) File "c:\python25\lib\site-packages\MySQLdb\cursors.py" in execute 151. query = query % db.literal(args) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in literal 247. return self.escape(o, self.encoders) File "c:\python25\lib\site-packages\MySQLdb\connections.py" in string_literal 180. return db.string_literal(obj) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

-568~-~~--~~-~~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-***@googlegroups.com To unsubscribe from this group, send email to django-users-***@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~~-

Now this all works fine till I get a non English PC using this (the error below is from our very basic contact form!) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

how do you compose your email? I had this problem even using postgresql when I tried to compose the email within the view by concatenating strings. I then set up a text template and passed variables to that - and the problem went away -- regards kg http://lawgon.livejournal.com http://nrcfosshelpline.in/code/

-596~-~~--~~-~~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-***@googlegroups.com To unsubscribe from this group, send email to django-users-***@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~~-

we use this http://www.djangosnippets.org/snippets/285/ as a base and made it work for us. templates are stored in the database so our users can "edit" the html email and text email templates with a list of "variables" they can use. Works quite well .. and now even with out acii/unicode errors by the looks of it. you can see most of the code for this in the dpast link in my 1st message

Now this all works fine till I get a non English PC using this (the error below is from our very basic contact form!) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

how do you compose your email? I had this problem even using postgresql when I tried to compose the email within the view by concatenating strings. I then set up a text template and passed variables to that - and the problem went away -- regards kghttp://lawgon.livejournal.comhttp://nrcfosshelpline.in/code/

-624~-~~--~~-~~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-***@googlegroups.com To unsubscribe from this group, send email to django-users-***@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~~-

I also recommend you to use Django's EmailMessage class and its derivatives [1], as they handle unicode perfectly; they also reduce your code for sending emails to a couple of lines, which is always good.

[1] http://www.djangoproject.com/documentation/email/

e-mail-messages

we use thishttp://www.djangosnippets.org/snippets/285/as a base and made it work for us. templates are stored in the database so our users can "edit" the html email and text email templates with a list of "variables" they can use. Works quite well .. and now even with out acii/unicode errors by the looks of it. you can see most of the code for this in the dpast link in my 1st message

Now this all works fine till I get a non English PC using this (the error below is from our very basic contact form!) Exception Type: UnicodeEncodeError at /contact-opal-students/ Exception Value: 'ascii' codec can't encode character u'\xb4' in position 88: ordinal not in range(128)

how do you compose your email? I had this problem even using postgresql when I tried to compose the email within the view by concatenating strings. I then set up a text template and passed variables to that - and the problem went away -- regards kghttp://lawgon.livejournal.comhttp://nrcfosshelpline.in/code/

-656~-~~--~~-~~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-***@googlegroups.com To unsubscribe from this group, send email to django-users-***@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~~-

What is an error in ASCII encoding?

An error occurs when an attempt is made to save characters outside the range (or representable range) of an encoding scheme because code points outside the encoding scheme’s upper bound (for example, ASCII has a 256 range) do not exist. An error would be produced by values greater than +127 or -128.

Can 0xC3 be an ASCII encoding?

ASCII characters codes go from 0x00 to 0x7F. What you have there is a character code 0xC3, which cannot be an ASCII-encoded character. Somewhere you should be able to tell it which encoding to use, e.g. in UTF-8 it would represent Ã.

Why can't I encode a non

Though you should avoid such representation and configure your environment to accept non-ascii characters instead -- it is 2016! Late answer, but this error is related to your terminal's encoding not supporting certain characters.

Why can't I encode a string in Python?

Voil\u00E0! The issue is that when you call str (), python uses the default character encoding to try and encode the bytes you gave it, which in your case are sometimes representations of unicode characters. To fix the problem, you have to tell python how to deal with the string you give it by using .encode ('whatever_unicode').