I have Django app hosted on Azure that is connected to MySQL database (Azure Database for MySQL). I wanted to edit my profile so I put ść (for testing purposes) in First name and I got the following error:
OperationalError at /edit_profile/
(1366, "Incorrect string value: '\\xC5\\x9B\\xC4\\x87' for column 'first_name' at row 1")
Request Method: POST
Request URL: http://127.0.0.1:8000/edit_profile/
Django Version: 3.2
Exception Type: OperationalError
Exception Value:
(1366, "Incorrect string value: '\\xC5\\x9B\\xC4\\x87' for column 'first_name' at row 1")
Traceback Switch to copy-and-paste view
C:\Users\myname\Anaconda3\lib\site-packages\django\db\backends\utils.py, line 84, in _execute
return self.cursor.execute(sql, params) …
▶ Local vars
C:\Users\myname\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py, line 73, in execute
return self.cursor.execute(query, args) …
▶ Local vars
C:\Users\myname\Anaconda3\lib\site-packages\MySQLdb\cursors.py, line 206, in execute
res = self._query(query) …
▶ Local vars
C:\Users\myname\Anaconda3\lib\site-packages\MySQLdb\cursors.py, line 319, in _query
db.query(q) …
▶ Local vars
C:\Users\myname\Anaconda3\lib\site-packages\MySQLdb\connections.py, line 259, in query
_mysql.connection.query(self, query) …
▶ Local vars
The above exception ((1366, "Incorrect string value: '\\xC5\\x9B\\xC4\\x87' for column 'first_name' at row 1")) was the direct cause of the following exception:
C:\Users\myname\Anaconda3\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
My server parameners on Azure are:
character_set_server = utf8mb4 (utf8 is not working too)
collation_server = utf8_general_ci
From what I know Django uses utf-8 by default so my question is what can I do to let users use Polish, French, German etc. letters in their usernames, first names and last names?
EDIT:
The solution to this problem was much simpler than I thought. I checked enconding in all tables and apparently the default encoding was set up to latin1_swedish_ci
instead of uft8mb4
. I specified it in my database options:
'OPTIONS': {
'charset': 'utf8mb4',
'init_command': 'SET character_set_connection=utf8mb4;'
'SET collation_connection=utf8mb4_unicode_ci;'
"SET NAMES 'utf8mb4';"
"SET CHARACTER SET utf8mb4;"},
and changed enconding in all existiong tables.