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/ |
+--------------------------+----------------------------+