Django1.8:UnicodeEncodeError'gbk'编解码器无法编码字符'\ xbb'

时间:2015-05-19 01:46:47

标签: python django python-3.x encoding django-1.8

我正在使用django 1.8,在windows7中开发。

系统编解码器是'gbk'。

我打电话的时候:

print(request.POST)

在我的视图功能中,我收到此错误:

UnicodeEncodeError at /mrp/list/item/purchase/0/
'gbk' codec can't encode character '\xbb' in position 206: illegal multibyte sequence

我注意到调试信息显示META['PYTHONIOENCODING'] = 'GBK'

但是当我尝试在错误代码之前打印python编码时:

import sys
print(sys.getdefaultencoding())

我显示utf8

那么为什么它使用'GBK'?我应该在哪里更改设置?我一直在寻找文件,但没有运气。

请帮忙!

META:

Variable    Value
SYSTEMROOT  
'C:\\Windows'
PATH_INFO   
'/mrp/list/item/purchase/0/'
HOMEPATH    
'\\Users\\Alfred'
WINDOWS_TRACING_LOGFILE 
'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log'
HTTP_ACCEPT_LANGUAGE    
'zh-CN,zh;q=0.8,en;q=0.6,ru;q=0.4,zh-TW;q=0.2'
DJANGO_SETTINGS_MODULE  
'ecerp.settings'
SHIM_MCCOMPAT   
'0x810000001'
USERDOMAIN  
'Alfred-PC'
SERVER_NAME 
'activate.adobe.com'
PROCESSOR_IDENTIFIER    
'Intel64 Family 6 Model 58 Stepping 9, GenuineIntel'
WINDOWS_TRACING_FLAGS   
'3'
FP_NO_HOST_CHECK    
'NO'
wsgi.file_wrapper   
''
QUERY_STRING    
''
CLASSPATH   
'.;C:\\Program Files\\Java\\jdk1.8.0_31\\lib;C:\\Program '
'Files\\Java\\jdk1.8.0_31\\lib\\tools.jar;d:\\java\\lib'
SERVER_PROTOCOL 
'HTTP/1.1'
PYTHONIOENCODING    
'GBK'
ALLUSERSPROFILE 
'C:\\ProgramData'
USERNAME    
'Alfred'
NUMBER_OF_PROCESSORS    
'4'
PROGRAMFILES(X86)   
'C:\\Program Files (x86)'
HTTP_PRAGMA 
'no-cache'
PSMODULEPATH    
'C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\'
TMP 
'C:\\Users\\Alfred\\AppData\\Local\\Temp'
COMPUTERNAME    
'ALFRED-PC'
SESSIONNAME 
'Console'
USERPROFILE 
'C:\\Users\\Alfred'
SERVER_PORT 
'8000'
PROCESSOR_ARCHITECTURE  
'AMD64'
REMOTE_HOST 
''
HTTP_COOKIE 
'verify_code=eW91Y2h1YW5n; sessionid=pncf4qix2xxjvkig3ek9v9oyvgb7aouc; '
'csrftoken=daIMsNDpBbireeQsbVGRJ6tMYijwxAu8'
wsgi.multithread    
True
wsgi.input  
<_io.BufferedReader name=908>
PATHEXT 
'.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY'
PROGRAMW6432    
'C:\\Program Files'
PYCHARM_HOSTED  
'1'
COMMONPROGRAMW6432  
'C:\\Program Files\\Common Files'
HTTP_ORIGIN 
'http://localhost:8000'
WINDIR  
'C:\\Windows'
GATEWAY_INTERFACE   
'CGI/1.1'
PROGRAMDATA 
'C:\\ProgramData'
wsgi.multiprocess   
False
REQUEST_METHOD  
'POST'
OS  
'Windows_NT'
SERVER_SOFTWARE 
'WSGIServer/0.2'
HTTP_CONNECTION 
'keep-alive'
SYSTEMDRIVE 
'C:'
PYTHONPATH  
'D:\\app\\ecerp'
GRANDE  
'C:\\Program Files (x86)\\Fuji Xerox\\DocuPrint SSW2\\SimpleMonitor for AP'
JAVA_HOME   
'C:\\Program Files\\Java\\jdk1.8.0_31'
wsgi.run_once   
False
HTTP_REFERER    
'http://localhost:8000/mrp/list/item/purchase/0/'
COMMONPROGRAMFILES  
'C:\\Program Files\\Common Files'
LOGONSERVER 
'\\\\ALFRED-PC'
PUBLIC  
'C:\\Users\\Public'
COMSPEC 
'C:\\Windows\\system32\\cmd.exe'
HTTP_HOST   
'localhost:8000'
TEMP    
'C:\\Users\\Alfred\\AppData\\Local\\Temp'
wsgi.errors 
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='GBK'>
PROCESSOR_REVISION  
'3a09'
COMMONPROGRAMFILES(X86) 
'C:\\Program Files (x86)\\Common Files'
HTTP_CACHE_CONTROL  
'no-cache'
REMOTE_ADDR 
'127.0.0.1'
HTTP_ACCEPT 
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
PROGRAMFILES    
'C:\\Program Files'
HTTP_ACCEPT_ENCODING    
'gzip, deflate'
CONTENT_LENGTH  
'1266'
wsgi.url_scheme 
'http'
HOMEDRIVE   
'C:'
PROCESSOR_LEVEL 
'6'
HTTP_USER_AGENT 
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/41.0.2272.118 Safari/537.36'
RUN_MAIN    
'true'
LOCALAPPDATA    
'C:\\Users\\Alfred\\AppData\\Local'
#ENVTSLOGSSS2364    
'3014800'
PYTHONUNBUFFERED    
'1'
CSRF_COOKIE 
'daIMsNDpBbireeQsbVGRJ6tMYijwxAu8'
CONTENT_TYPE    
'application/x-www-form-urlencoded'
PATH    
'C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Intel\\iCLS '
'Client\\;C:\\Program Files\\Intel\\iCLS '
'Client\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program '
'Files (x86)\\Intel\\OpenCL SDK\\2.0\\bin\\x86;C:\\Program Files '
'(x86)\\Intel\\OpenCL SDK\\2.0\\bin\\x64;C:\\Program Files\\Intel\\Intel(R) '
'Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) '
'Management Engine Components\\IPT;C:\\Program Files (x86)\\Intel\\Intel(R) '
'Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) '
'Management Engine Components\\IPT;C:\\Program Files (x86)\\Bitvise SSH '
'Client;C:\\Python34;C:\\Python34\\Scripts;C:\\Dev-Cpp\\bin;C:\\Program Files '
'(x86)\\QuickTime\\QTSystem\\;C:\\Program '
'Files\\TortoiseGit\\bin;C:\\Python34\\Scripts;C:\\Program '
'Files\\nodejs;D:\\upupw\\MySQL\\bin;C:\\Users\\Alfred\\AppData\\Roaming\\npm;C:\\Program '
'Files\\Java\\jdk1.8.0_31\\bin;C:\\Development\\android-sdk\\platform-tools;C:\\Development\\android-sdk\\tools'
SCRIPT_NAME 
''
wsgi.version    
(1, 0)
APPDATA 
'C:\\Users\\Alfred\\AppData\\Roaming'

1 个答案:

答案 0 :(得分:0)

最后我发现这是因为文件编码错误。

Windows下新更新的Pycharm IDE更改了默认编码。

所以我的新代码文件编码转为GBK,应该是utf8。

我更改了IDE设置,更改了文件编码,一切都很顺利。