Django的中文編碼問題

來源:互聯網
上載者:User

Django真的很強大,用了很短的時間就搭了一個簡易的Blog,問題又來了,中文內容存入Mysql中是亂碼。

經查,Django預設是採用UTF-8編碼的,相關檔案的格式也都是UTF-8的,問題應該出在Mysql資料庫上面。

我是在Mac OS10.6.2上裝的64位mysql,預設編碼,沒改過

進入mysql,通過status命令查看,果然,編碼是Latin1

將其改為UTF-8即可解決

單獨修改某個資料庫可用alter database testdb character set utf8;

統一修改的話,需要在/usr/local/mysql/support-files下[目錄因系統而異],找到下列檔案 

my-huge.cnf 
my-large.cnf 
my-small.cnf 
... 
這是設定檔範本,copy到/etc/my.cnf,就可以使用這個設定檔,我用了small檔案。

在[mysqld]和[client]兩個標籤下都加入 default_character_set = utf8

重新啟動mysql,status結果如下:

mysql> status

--------------

mysql  Ver 14.14 Distrib 5.1.44, for apple-darwin10.2.0 (i386) using readline 5.1

Connection id:43

Current database:

Current user:root@localhost

SSL:Not in use

Current pager:stdout

Using outfile:''

Using delimiter:;

Server version:5.1.44 MySQL Community Server (GPL)

Protocol version:10

Connection:Localhost via UNIX socket

Server characterset:utf8

Db     characterset:utf8

Client characterset:utf8

Conn.  characterset:utf8

UNIX socket:/tmp/mysql.sock

Uptime:1 hour 4 min 20 sec

Threads: 1  Questions: 238  Slow queries: 0  Opens: 50  Flush tables: 1  Open tables: 4  Queries per second avg: 0.61

--------------

再次運行程式,中文成功持久化。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.