django管理后台,如何支持中文提交?!
models.py代码:
请输入代码
#coding:utf-8
from django.db import models
from django.contrib import admin
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()
class Meta:
ordering = ('-timestamp',)
class BlogPostAdmin(admin.ModelAdmin):
list_display = ('title','timestamp')
admin.site.register(BlogPost,BlogPostAdmin)
后台界面:
提交后错如下:
Warning at /admin/blog/blogpost/add/
Incorrect string value: '\xE4\xB8\xAD\xE6\x96\x87...' for column 'title' at row 1
Request Method: POST
Request URL: http://172.16.41.7/admin/blog/blogpost/add/
Django Version: 1.6.5
Exception Type: Warning
Exception Value:
Incorrect string value: '\xE4\xB8\xAD\xE6\x96\x87...' for column 'title' at row 1
Exception Location: C:\Python27\lib\site-packages\MySQLdb\cursors.py in _warning_check, line 92
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Python Path:
['D:\\web',
'C:\\Python27\\lib\\site-packages\\pip-1.4.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\setuptools-3.4.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\requests-2.2.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\psutil-2.1.3-py2.7-win-amd64.egg',
'C:\\Python27\\lib\\site-packages\\django_admin_bootstrapped-2.0.4-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\simplejson-3.6.3-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pycharm-debug.egg',
'C:\\Python27\\lib\\site-packages\\django_xadmin-0.5.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\django_crispy_forms-1.4.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\django_reversion-1.8.4-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\django_jinja-1.0.4-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Users\\shine_forever\\AppData\\Roaming\\Python\\Python27\\site-packages',
'C:\\Python27\\lib\\site-packages']
Server time: 星期二, 4 十一月 2014 14:12:55 +0800
Traceback Switch to copy-and-paste view
如果管理界面输入因为就没有问题!
mysql也设置为utf-8了:
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/local/mysql-5.1.65-linux-x86_64-glibc23/share/charsets/ |
+--------------------------+--------------------------------------------------------------+
8 rows in set (0.00 sec)
数据库显示乱码为:
mysql> select * from blog_blogpost;
+----+---------------+-----------------+---------------------+
| id | title | body | timestamp |
+----+---------------+-----------------+---------------------+
| 1 | first test | first test | 2014-11-03 03:19:45 |
| 2 | second | second 22222 | 2014-11-03 03:32:04 |
| 3 | third 3333333 | third?????????? | 2014-11-03 03:32:27 |
| 11 | ???? | ?? | 2014-11-04 06:11:38 |
+----+---------------+-----------------+---------------------+
4 rows in set (0.00 sec)
请问这个问题如何解决啊?!
一瓶蛋疼的酱油
10 years, 3 months ago
Answers
目测是提交表单的时候,并没有提交
unicode
的内容。
可以现在
settings.py
设置
LANGUAGE_CODE = 'zh-cn'
将语言使用中文试试。
还有就是创建数据库的时候不是合适的
utf-8
,可以重新建立数据库:
DROP DATABASE IF EXISTS `databasename`;
CREATE DATABASE `databasename` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
USE 'mysql';
GRANT ALL PRIVILEGES ON databasename.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
魂狩D骑士
answered 10 years, 3 months ago