Django支援中文MySQL資料庫與中文網頁

Step1)編輯Django的setting.py支援繁體中文。

編輯project/project/setting.py修改LANGUAGE_CODE為zh-Hant

LANGUAGE_CODE = 'zh-Hant'

Step2)修改資料庫models.py,加上函式__unicode__

def __unicode__(self):

return self.name

Step3)修改MySQL資料庫使用utf8編碼

修改/etc/my.cnf的mysqld部份加上

character-set-server=utf8

init-connect="SET NAMES utf8"

collation-server=utf8_unicode_ci

重新啟動mysqld

#service mariadb restart

Step4)在mysql新增utf8編碼資料庫

使用「mysql -u root -p」與密碼登入mysql資料庫,在資料庫命令列新增utf8編碼的資料庫,django使用此utf8資料庫。

create database django_db_name default character set utf8;