Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django charset and encoding

I am trying to store and view greek characters from/to a Mysql db.

I want to use utf8 encoding i alter my db with the following command:

ALTER DATABASE el CHARACTER SET utf8 COLLATE utf8_general_ci

Now when i am trying to create an article i see the following error:

error page

When i am trying to fetch the article from my db i see something like:

mysql> select * from article_article
    -> ;
+----+------------+----------+-------------+----------------------------------------------------------------------------------+----------+------+-------+-------+--------------+---------------------+---------------------+-----------+
| id | submenu_id | title    | url         | body                                                                             | image_id | hits | votes | grade | publisher_id | time_upload         | last_modified       | published |
+----+------------+----------+-------------+----------------------------------------------------------------------------------+----------+------+-------+-------+--------------+---------------------+---------------------+-----------+
|  1 |          1 | ???????? | ssss-d-ssss | <p>&epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;?</p>
<p>&nbsp;</p>
<p>?</p> |        9 |    0 |     0 |   2.5 |            1 | 2013-04-11 10:39:30 | 2013-04-11 11:02:01 |         1 |
+----+------------+----------+-------------+----------------------------------------------------------------------------------+----------+------+-------+-------+--------------+---------------------+---------------------+-----------+
1 row in set (0.00 sec)

I use tinymce for bodyfield..(Tinymce save greek characters very bad)

I also check that in admin page something like the following is missing:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

How can i configure django for utf8 format? How can i configure tinymce for Emding Greek Chars normally?

Edit: changing the collate of the field with mysql commands solve the problem. Also changing entity_encoding from named to raw in tiny_mce.js solve and the second problem.The only problem now is that the warning doesn't disapear :(

like image 749
Chris P Avatar asked Dec 27 '22 05:12

Chris P


1 Answers

Set the charset option to utf8mb4 in the OPTIONS dict of the DATABASES setting in the Django settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_db',
        'USER': 'my_user',
        'PASSWORD': 'my_pass',
        'HOST': 'my.host',
        'OPTIONS': {
            'charset': 'utf8mb4'  # This is the relevant line
        }
    }
}

These options will be passed as arguments to the mysql driver connection function, which typically is mysqlclient. Here you can see in the documentation all these parameters:

https://mysqlclient.readthedocs.io/user_guide.html#functions-and-attributes

If you use a different driver for mysql though, you might need a different option.

Also make sure that in the MySQL database, the charset of the either the column, the table or the whole database is set to utf8mb4. For more info about character sets in MySQL see:

MySQL docs: Specifying Character Sets and Collations
MySQL docs: The utf8mb4 Character Set (4-Byte UTF-8 Unicode Encoding)
Stack Overflow: What is the difference between utf8mb4 and utf8 charsets in MySQL?

There BTW an ongoing pull-request in Django to make utf8mb4 the default: https://github.com/django/django/pull/8886

like image 154
gitaarik Avatar answered Dec 28 '22 21:12

gitaarik