私は自分のdjangoプロジェクトでSMSを送信しようとしています.これは私のSMS関数です.
mit2sms.py
import urllib
import urllib2
def sendsms(phonenumber,textcontent):
textcontent=textcontent.decode('utf8').encode('big5') #10
#textcontent=textcontent.encode('big5') #9
url = "https://urls?username=myname&password=mypassword&dstaddr="+phonenumber+"&smbody="+textcontent
req = urllib2.Request(url)
response = urllib2.urlopen(req)
Pythonシェルで中国語と英語のSMSを送信できますが、それをdjangoプロジェクトにインポートすると、
'ascii' codec can't decode byte 0xb7 in position 0: ordinal not in range(128)
エラー
私のジャンゴ
ビュー.py
#-*- coding: utf-8 -*-
....
from mit2sms import * # I import the sms function here
....
def register(request):
....
tel = request.POST['tel']
textcontent = "會員申請通過"
sendsms(tel,textcontent)
....
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)