0
点赞
收藏
分享

微信扫一扫

【django】用户重复注册校验功能接口实现【12】

写心之所想 2022-04-24 阅读 96

在这里插入图片描述

一、⽤户名重复注册接⼝设计和定义

1、请求⽅式
在这里插入图片描述
2、请求参数:路径参数
在这里插入图片描述
3、响应结果:JSON
在这里插入图片描述

二、定义路由

主路由

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())
]

三、定义视图

class UsernameCount(View):
    def get(self,request,username):
        '''
        查询当前用户名的记录数
        :param request:
        :param username:
        :return:
        '''

        count=Users.objects.filter(username=username).count()
        return JsonResponse({'code':200,'msg':'ok','count':count})

四、前端代码

if(!this.error_username){
    axios.get('/usernames/'+this.username+'/count/',{
        responseType:'json'
    }).then(response=>{
        if(response.data.count==1){
            this.error_username_msg = '当前用户名已经注册';
            this.error_username = true;
        }else{
            this.error_username = false;
        }
    }).catch(error=>{
        console.log(error.response);
    });
}
举报

相关推荐

0 条评论