Django ';ascii';编解码器可以';t解码位置0处的字节0xb7:序号不在范围内(128)
我试图在我的django项目中发送短信,这是我的短信功能 mit2sms.py 我可以用pythonshell发送中英文短信,但是当我把它导入到我的django项目时,我得到了Django ';ascii';编解码器可以';t解码位置0处的字节0xb7:序号不在范围内(128),django,python-2.7,utf-8,big5,Django,Python 2.7,Utf 8,Big5,我试图在我的django项目中发送短信,这是我的短信功能 mit2sms.py 我可以用pythonshell发送中英文短信,但是当我把它导入到我的django项目时,我得到了 'ascii' codec can't decode byte 0xb7 in position 0: ordinal not in range(128) 错误 我的django views.py views.py和mit2sms.py位于同一目录中 但在我注册之后,我犯了一个错误 Environment: Req
'ascii' codec can't decode byte 0xb7 in position 0: ordinal not in range(128)
错误
我的django
views.py
views.py和mit2sms.py位于同一目录中
但在我注册之后,我犯了一个错误
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/register/
Django Version: 1.9.7
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'member']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/korekyourin/books/stayreal/sandbox/member_register/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Users/korekyourin/books/stayreal/sandbox/member_register/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/korekyourin/books/stayreal/sandbox/member_register/member_register/member/views.py" in register
57. sendsms(tel,textcontent)
File "/Users/korekyourin/books/stayreal/sandbox/member_register/member_register/member/mit2sms.py" in sendsms
10. url = "https://urls?username=myname&password=mypassword&dstaddr="+phonenumber+"&smbody="+textcontent
Exception Type: UnicodeDecodeError at /register/
Exception Value: 'ascii' codec can't decode byte 0xb7 in position 0: ordinal not in range(128)
请尝试修复此行:
textcontent = "會員申請通過"`
致:
请尝试修复此行:
textcontent = "會員申請通過"`
致:
当您将str隐式转换为Unicode时,您往往会得到
UnicodeDecodeError UnicodeDecodeError:“ascii”编解码器无法解码
在以下情况下可能发生这种情况:
unicode('€') # explicit conversion without encoding
u"The currency is: {}".format('€') # new style format string into Unicode string - Python will try to convert value string to Unicode first
u'The currency is: %s' % '€' # old style format string into Unicode string - Python will try to convert value string to Unicode first
u'The currency is: ' + '€' # append string to Unicode - Python will try to convert string to Unicode first
后者很可能就是您要解决的问题——将str附加到Unicode。Python将首先尝试将str转换为Unicode,但将只使用“ascii”编解码器(Python2.x),这对于非ascii内容将失败
这不是很明显,但很可能是从以下位置获得Unicode对象:
tel = request.POST['tel']
然后,当您将tel
/phonenumber
(Unicode对象)添加到textcontent
(您的big5编码str)中时,您将得到UnicodeDecodeError
答案是删除任何隐含的转换,并删除不必要的解码/编码
更改:
textcontent = u"會員申請通過" # Note the 'u`. textcontent is now a Unicode
并将sendsms()
更改为:
def sendsms(phonenumber,textcontent):
url = u"https://urls?username=myname&password=mypassword&dstaddr="+phonenumber+"&smbody="+textcontent
# now a unicode also
# better written using String.format():
# url = u"https://urls?username=myname&password=mypassword&dstaddr={phonenumber}&smbody={textcontent}".format(phonenumber=phonenumber, textcontent=textcontent)
req = urllib2.Request(url.encode('big5')) # encode only when absolutely necessary
response = urllib2.urlopen(req)
当您将str隐式转换为Unicode时,您往往会得到UnicodeDecodeError UnicodeDecodeError:“ascii”编解码器无法解码
在以下情况下可能发生这种情况:
unicode('€') # explicit conversion without encoding
u"The currency is: {}".format('€') # new style format string into Unicode string - Python will try to convert value string to Unicode first
u'The currency is: %s' % '€' # old style format string into Unicode string - Python will try to convert value string to Unicode first
u'The currency is: ' + '€' # append string to Unicode - Python will try to convert string to Unicode first
后者很可能就是您要解决的问题——将str附加到Unicode。Python将首先尝试将str转换为Unicode,但将只使用“ascii”编解码器(Python2.x),这对于非ascii内容将失败
这不是很明显,但很可能是从以下位置获得Unicode对象:
tel = request.POST['tel']
然后,当您将tel
/phonenumber
(Unicode对象)添加到textcontent
(您的big5编码str)中时,您将得到UnicodeDecodeError
答案是删除任何隐含的转换,并删除不必要的解码/编码
更改:
textcontent = u"會員申請通過" # Note the 'u`. textcontent is now a Unicode
并将sendsms()
更改为:
def sendsms(phonenumber,textcontent):
url = u"https://urls?username=myname&password=mypassword&dstaddr="+phonenumber+"&smbody="+textcontent
# now a unicode also
# better written using String.format():
# url = u"https://urls?username=myname&password=mypassword&dstaddr={phonenumber}&smbody={textcontent}".format(phonenumber=phonenumber, textcontent=textcontent)
req = urllib2.Request(url.encode('big5')) # encode only when absolutely necessary
response = urllib2.urlopen(req)
如果将textcontent定义为unicode,它是否有效<代码>文本内容=u“會員申請通過"代码>获取错误异常值但异常位置不同:异常值:“ascii”编解码器无法对位置0-5中的字符进行编码:序号不在范围(128)内异常位置:/Users/ccl/sandbox/member_register/venv/lib/python2.7/encodings/utf_8.py在decode中,第16行尝试在这两个文件中导入此内容。从将来导入unicode_literals时,如果将textcontent定义为unicode,它是否工作?textcontent=u“會員申請通過“
获取错误异常值但异常位置不同:异常值:“ascii”编解码器无法对位置0-5中的字符进行编码:序号不在范围内(128)异常位置:/Users/ccl/sandbox/member_register/venv/lib/python2.7/encodings/utf_8.py在解码中,第16行尝试在两个文件中导入此项。从以后的导入unicode_文本