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/(?P0[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/(?P0[7-9]\d{9})/$',views.SMSCodeView.as_view()),#发短信
    # re_path(r'^sms/code', SMsCodeView.as_view()),#发短信
]

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


http://127.0.0.1:8000/api/sms/code/?mobile=09023131315

请求的url不对吧,根据路由匹配规则的定义re_path(r'^sms/code/(?P0[7-9]\d{9})/$',views.SMSCodeView.as_view()),mobile只是命名分组的名字,url请求不用带。改成http://127.0.0.1:8000/api/sms/code/?09023131315/%E8%AF%95%E8%AF%95

有帮助的话,请点采纳~

img


检查下这里anmo_app1.urls的路由配置,api/ 和sms之间有存在空格, 应该是include的时候命名空间后面有空格

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^