2301_76784835 2023-03-02 12:21 采纳率: 33.3%
浏览 40
已结题

django 开发api接口,视图路由报 404 not found 怎么解决

我正在学习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()),#发短信
]

  • 写回答

4条回答 默认 最新

  • Web Security Loop 2023-03-02 12:35
    关注

    看你一下报错的显示,Django在处理请求时找不到匹配的URL模式。在你的URL模式中,正则表达式好像没有匹配到请求的URL。你可以检查一下请求URL的格式是否正确,因为它似乎包含了一个不必要的反斜杠(/)。可以尝试删除URL中的斜杠。

    
    http://127.0.0.1:8000/api/sms/code/?mobile=09023131315
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 创建了问题 3月2日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果