【django】用户手机号重复注册校验【14】

在这里插入图片描述

1、⼿机号重复注册接⼝设计和定义

请求⽅式
在这里插入图片描述
请求参数:路径参数
在这里插入图片描述

响应结果:JSON

在这里插入图片描述

2、定义路由和视图

主路由

from django.contrib import admin
from django.urls import path,re_path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^',include('userapp.urls')),
    re_path('^',include(('newsapp.urls','newsapp'),namespace='newsapp'))
]

子路由

from django.urls import path,re_path
from . import views

urlpatterns=[
    re_path('^register/$',views.RegisterView.as_view()),
    re_path('^usernames/(?P<username>[a-zA-Z_]{5,8})/count/$',views.UsernameCount.as_view()),
    re_path('^logout/$',views.LogoutView.as_view()),
    re_path('^phones/(?P<phone>1[3589][0-9]{9})/count/$',views.PhoneCountView.as_view()),
]

3、⼿机号重复注册后端逻辑

class PhoneCountView(View):
    def get(self,request,phone):
        '''
        查询手机号的记录数
        :param request:
        :return:
        '''
        count=Users.objects.filter(phone=phone).count()
        return JsonResponse({'code':200,'msg':'ok','count':count})

4、⼿机号重复注册前端逻辑

 // 2.手机号是否重复注册校验
if(!this.error_phone){
    axios.get('/phones/'+this.phone+'/count/',{
        responseType:'json'
    }).then(response=>{
        if(response.data.count==1){
            this.error_phone_msg = '手机号已经被注册';
            this.error_phone = true;
        }else{
            this.error_phone = false;
        }
    }).catch(error=>{
        console.log(error.response);
    });
}
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码敲到头发茂密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值