在django如何使用中文

weixin_30488085發表於2020-04-06
1) 首先,在你需要使用中文的py檔案中,都加上#coding=utf-8。

2) 在django的設定檔案裡,將預設的LANGUAGE_CODE = 'en-us'修改一下,改成'zh-cn'。

3) linux下mysql預設的字符集並非utf-8編碼,需要進行設定,將字符集改為utf-8編碼。

網上搜到的相關方法是這樣的:

預設登入mysql之後show variables like '%character%', 出現
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                       |
| character_set_connection | latin1                       |
| character_set_database   | latin1                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                       |
| character_set_server     | latin1                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

修改/etc/mysql/my.cnf
在[client]下增加default-character-set=utf8
在[mysqld]下增加default-character-set=utf8
同時加上init_connect='SET NAMES utf8' (設定連線mysql資料庫時使用utf8編碼,以讓mysql資料庫為utf8執行)
重新啟動mysql,執行:sudo /etc/init.d/mysql stop 然後sudo /etc/init.d/mysql start
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

轉載於:https://www.cnblogs.com/cly84920/archive/2010/10/13/4426665.html

相關文章