Django+MySQL: How To Fix Unicode (aka Mysterious Question Marks)
If you’re running into the problem where unicode items in your Django / MySQL project are displayed as question marks, here’s the likely problem and solution, found in this django-users thread:
The likely problem is that your MySQL encoding is set to latin1, as opposed to utf8. You can check this via:
mysqld --verbose --help | grep character-set
You’ll probably see:
character-set-server latin1
You want this to be uft8. To modify it, edit your my.conf file ( /etc/mysql/my.conf on ubuntu ), adding the following lines to the appropriate sections:
[client]
...
default-character-set = utf8
[mysqld]
...
character-set-server=utf8
collation-server=utf8_unicode_ci
init_connect='set collation_connection = utf8_unicode_ci;'
Now restart mysql:
sudo /etc/init.d/mysql restart
And alter your existing tables to use the utf8 encoding:
mysql your_db_name
alter table your_table_name convert to character set utf8;
And that should do it.
Manage your expenses via Email, SMS, iPhone, Twitter, Voice (Call and say your expense), IM (Yahoo, AIM, MSN), or Web.
Comments(5)
[...] Django+MySQL: How To Fix Unicode (aka Mysterious Question Marks) – Standard Deviations (tags: mysql django python unicode) Category: del.icio.us | Comment (RSS) | Trackback [...]
I just wanted to give you a HUGE THANK YOU for posting these instructions. I had migrated a MySQL database from 3.23 to 5.x, using mysqldump. With the new server, some PHP pages started showing “?” in place of certain characters, like quotes and em-dashes. I simply edited my.cnf on the MySQL server, restarted, and all is well! You saved me a ton of time digging around to fix this!
MILLION thanks mate, big up!
YOUR A LEGEND, been having so much trouble using tinymce for a TextField on my blog model this seems to have solved the problem!! thanks again!
million and one thanks!