Django version 2.0. Python 3
My database charset and collation:
mysql> SELECT @@character_set_database, @@collation_database;
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| latin1 | latin1_swedish_ci |
+--------------------------+----------------------+
Old developer inserted data in KOI8-R encoding using Perl :(
To get correct values from database I used ugly construction str(username).encode('latin1').decode('koi8-r')
. And what? I need to use it in all my project to send data to output? Or write function to encode context dictionary, but i also need additional to encode/decode all data. It will affect the usability and productivity
Without this i get something like ëÏÚÌÑÎËÏ òÏÍÁÎ éÏÓÉÆÏ×ÉÞ
How to globally set encoding in Django to prevent encode/decode operation in every place? I changed encoding different ways and nothing happens.
In settings.py I tried to set DEFAULT_CHARSET into different encodings (if I set default_charset to KOI8-R i get an error: UnicodeEncodeError: 'charmap' codec can't encode characters in position 6228-6235: character maps to . With other encodings no errors but no result). I tried to set in Database section of settings.py different values of charset and collation.
'OPTIONS': {
'charset': 'latin1',
'init_command': "SET sql_mode='STRICT_TRANS_TABLES', character_set_client=latin1, character_set_results=latin1, character_set_connection=latin1, collation_connection=latin1_swedish_ci",
}
I added <meta http-equiv="Content-type" content="text/html; charset=koi8-r (or other)" />
to <head>
tag in index.html template. No result.
It seems that Django execute SET NAMES utf8
everytime
Why in Perl i can send header with charset=koi8-r and i get normal values from this tables in my browser using CGI? Why no similar result in Python with Django or Flask? Simple example in Perl