settings.py中设置
python
##################################容联云短信平台账号信息##############################
#容联云查看信息
RONGLIAN_ACC_ID = '......'
RONGLIAN_ACC_TOKEN ='......'
RONGLIAN_APP_ID = '......'
#####################################################################
views中
python
from ronglian_sms_sdk import SmsSDK
from django.conf import settings
sms_sdk = SmsSDK(
accId=settings.RONGLIAN_ACC_ID,
accToken=settings.RONGLIAN_ACC_TOKEN,
appId=settings.RONGLIAN_APP_ID
)
#tid测试状态下只能为'1'
#mobile,多个号码以英文逗号分割
#data,发送的内容和时长,必须是元组
verify_code = random.randint(1000,9999)
expire = 10
res = sms_sdk.sendMessage(tid ='1', mobile=phone,datas=(verify_code,expire))
res_dict = json.loads(res)
statusCode = res_dict.get('statusCode')
statusMsg =res_dict.get('statusMsg')
if statusCode=='000000':
####################写入Redis###########################
from django_redis import get_redis_connection
# 默认连接配置文件中的库
redis_conn = get_redis_connection(alias='sms')
redis_conn.set(f'sms_{phone}', verify_code, ex=expire * 60)
########################################################
context = {
'code':200,
'data':'发送短信成功'
}
else:
context = {
'code': statusCode,
'data': statusMsg
}
return JsonResponse(context)