我正在学习django,学习开发一个发送验证码的视图,最开始是可以用的,后来就不能用了期间没改过代码。哪位朋友指导一下,谢谢。
相关代码如下
报错代码
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/api/sms/code/?mobile=09023131315/
Using the URLconf defined in anmo_app1.urls, Django tried these URL patterns, in this order:
admin/
api/ sms/code/(?P<mobile>0[7-9]\d{9})/$
The current path, api/sms/code/, didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
视图代码
class SMSCodeView(APIView):
"""短信验证码"""
@staticmethod
def get(request, mobile):
# 1 创建redis链接对象
redis_coon = get_redis_connection('verify_codes')
send_flag = redis_coon.get('send_flag_%s' % mobile)
if send_flag:
return Response({'message': '已经发送过短信'}, status=status.HTTP_400_BAD_REQUEST)
# 2 生成验证码
sms_code = '%06d' % randint(0, 999999)
print(sms_code)
# 创建 执行管道pl,把两次请求发在管道,只执行一次减少链接redis次数
# pl = redis_coon.pipeline()
# 3 把验证码存储到redis,300秒
# pl.setex('sms_%s' % mobile, const.SMS_CODE_REDIS_TIME, sms_code)
redis_coon.setex('sms_%s' % mobile, const.SMS_CODE_REDIS_TIME, sms_code)
# 4 存储一个是否发送过验证码的标记send_flag+手机号,60秒
# pl.setex('send_flag_%s' % mobile, const.SEND_SMS_CODE_INTERVAL, 1)
redis_coon.setex('send_flag_%s' % mobile, const.SEND_SMS_CODE_INTERVAL, 1)
# 执行管道pl
# pl.execute()
# 5 利用短信服务商发送短信 5分钟 SMS_CODE_REDIS_TIME 300秒// 6 = 5分钟 去celery的task列表
# CCP().send_template_sms(mobile,[sms_code,5],1)
# 6 出发异步任务函数
# send_sms_code.delay(mobile, sms_code)
return Response({'message': 'ok'})
视图路由代码,总路由已经配置完毕
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^sms/code/(?P<mobile>0[7-9]\d{9})/$',views.SMSCodeView.as_view()),#发短信
# re_path(r'^sms/code', SMsCodeView.as_view()),#发短信
]