先用的是MySQLdb
 创建的数据库的时候,没有设置字符类型,而导致了保存中文的时候报错
 重建数据库,然后ok

CREATE DATABASE myblog DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
 grant all privileges on myblog.* to app@'%' identified by 'allpass123'; 
 flush privileges;

具体报错信息如下:
 ```python

Article.objects.create(title = ‘Hello World’, category = ‘Python’, content = ‘我们来做一个简单的数据库增加操作’)
 Traceback (most recent call last):
 File “”, line 1, in 
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/manager.py”, line 127, in manager_method
 return getattr(self.get_queryset(), name)(*args, *kwargs)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/query.py”, line 348, in create
 obj.save(force_insert=True, using=self.db)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 710, in save
 force_update=force_update, update_fields=update_fields)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 738, in save_base
 updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 822, in savetable
 result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 861, in doinsert
 using=using, raw=raw)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/manager.py”, line 127, in manager_method
 return getattr(self.get_queryset(), name)(
args, *kwargs)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/query.py”, line 920, in insert
 return query.getcompiler(using=using).execute_sql(return_id)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/sql/compiler.py”, line 974, in execute_sql
 cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 79, in execute
 return super(CursorDebugWrapper, self).execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
 return self.cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/utils.py”, line 97, in
exit
 six.reraise(dj_exc_type, dj_exc_value, traceback)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
 return self.cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/mysql/base.py”, line 124, in execute
 return self.cursor.execute(query, args)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.11-intel.egg/MySQLdb/cursors.py”, line 205, in execute
 self.errorhandler(self, exc, value)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.11-intel.egg/MySQLdb/connections.py”, line 36, in defaulterrorhandler
 raise errorclass, errorvalue
 OperationalError: (1366, “Incorrect string value: ‘\xE6\x88\x91\xE4\xBB\xAC…’ for column ‘content’ at row 1”)
 Article.objects.create(title = ‘Hello World’, category = ‘Python’, content = ‘ok it is the content’)
 
 Article.objects.create(title = ‘Hello World’, category = ‘Python’, content = “ok it is the content不合理”)
 Traceback (most recent call last):
 File “”, line 1, in 
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/manager.py”, line 127, in manager_method
 return getattr(self.get_queryset(), name)(
args, *kwargs)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/query.py”, line 348, in create
 obj.save(force_insert=True, using=self.db)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 710, in save
 force_update=force_update, update_fields=update_fields)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 738, in save_base
 updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 822, in savetable
 result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 861, in doinsert
 using=using, raw=raw)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/manager.py”, line 127, in manager_method
 return getattr(self.get_queryset(), name)(
args, *kwargs)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/query.py”, line 920, in insert
 return query.getcompiler(using=using).execute_sql(return_id)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/sql/compiler.py”, line 974, in execute_sql
 cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 79, in execute
 return super(CursorDebugWrapper, self).execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
 return self.cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/utils.py”, line 97, in
exit
 six.reraise(dj_exc_type, dj_exc_value, traceback)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
 return self.cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/mysql/base.py”, line 124, in execute
 return self.cursor.execute(query, args)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.11-intel.egg/MySQLdb/cursors.py”, line 205, in execute
 self.errorhandler(self, exc, value)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.11-intel.egg/MySQLdb/connections.py”, line 36, in defaulterrorhandler
 raise errorclass, errorvalue
 OperationalError: (1366, “Incorrect string value: ‘\xE4\xB8\x8D\xE5\x90\x88…’ for column ‘content’ at row 1”)
 Article.objects.create(title = ‘Hello World’, category = ‘Python’, content = “ok it is the content不合理”)
 Traceback (most recent call last):
 File “”, line 1, in 
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/manager.py”, line 127, in manager_method
 return getattr(self.get_queryset(), name)(
args, *kwargs)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/query.py”, line 348, in create
 obj.save(force_insert=True, using=self.db)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 710, in save
 force_update=force_update, update_fields=update_fields)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 738, in save_base
 updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 822, in savetable
 result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/base.py”, line 861, in doinsert
 using=using, raw=raw)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/manager.py”, line 127, in manager_method
 return getattr(self.get_queryset(), name)(
args, **kwargs)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/query.py”, line 920, in insert
 return query.getcompiler(using=using).execute_sql(return_id)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/models/sql/compiler.py”, line 974, in execute_sql
 cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 79, in execute
 return super(CursorDebugWrapper, self).execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
 return self.cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/utils.py”, line 97, in exit
 six.reraise(dj_exc_type, dj_exc_value, traceback)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
 return self.cursor.execute(sql, params)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/django/db/backends/mysql/base.py”, line 124, in execute
 return self.cursor.execute(query, args)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.11-intel.egg/MySQLdb/cursors.py”, line 205, in execute
 self.errorhandler(self, exc, value)
 File “/Users/wangqi/virtualenv/mywebsite/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.11-intel.egg/MySQLdb/connections.py”, line 36, in defaulterrorhandler
 raise errorclass, errorvalue
 OperationalError: (1366, “Incorrect string value: ‘\xE4\xB8\x8D\xE5\x90\x88…’ for column ‘content’ at row 1”)
 exit
 Use exit() or Ctrl-D (i.e. EOF) to exit
Like what you read? Give ProfeeL a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.