Python-Django用户注册-判断账户是否存在

判断帐号是否存在
1. 判断用户名是否存在
后端接口设计:
请求方式: GET usernames/(?P<username>\w{5,20})/count/
请求参数: 路径参数
参数 类型 是否必传 说明
username str 用户名
返回数据: JSON
{
    "username": "itcast",
    "count": "1"
}
返回值 类型 是否必须 说明
username str 用户名
count int 数量
后端实现
在users/views.py中定义视图
# url(r'^usernames/(?P<username>\w{5,20})/count/$', views.UsernameCountView.as_view()), 
class UsernameCountView(APIView):
    """
    用户名数量
    """
    def get(self, request, username):
        """
        获取指定用户名数量
        """
        count = User.objects.filter(username=username).count()
        data = {
            'username': username,
            'count': count
        }
        return Response(data)
前端实现
在js/register.js中修改
    // 检查用户名
    check_username: function (){
            var len = this.username.length;
            if(len<5||len>20) {
                this.error_name_message = '请输入5-20个字符的用户名';
                this.error_name = true;
            } else {
                this.error_name = false;
            }
            // 检查重名
            if (this.error_name == false) {
                axios.get(this.host + '/usernames/' + this.username + '/count/', {
                        responseType: 'json'
                    })
                    .then(response => {
                        if (response.data.count > 0) {
                            this.error_name_message = '用户名已存在';
                            this.error_name = true;
                        } else {
                            this.error_name = false;
                        }
                    })
                    .catch(error => {
                        console.log(error.response.data);
                    })
            }
        },
2. 判断手机号是否存在:
后端接口设计:
请求方式: GET mobiles/(?P<mobile>1[3-9]\d{9})/count
请求参数: 路径参数
参数 类型 是否必须 说明
mobile str 手机号
返回数据: JSON
{
    "mobile": "18512345678",
    "count": 0
}
返回值 类型 是否必须 说明
mobile str 手机号
count int 数量
后端实现
在users/views.py中定义视图
# url(r'^mobiles/(?P<mobile>1[3-9]\d{9})/count/$', views.MobileCountView.as_view()),
class MobileCountView(APIView):
    """
    手机号数量
    """
    def get(self, request, mobile):
        """
        获取指定手机号数量
        """
        count = User.objects.filter(mobile=mobile).count()
        data = {
            'mobile': mobile,
            'count': count
        }
        return Response(data)
前端实现
在js/register.js中修改
    // 检查手机号
    check_phone: function (){
            var re = /^1[345789]\d{9}$/;
            if(re.test(this.mobile)) {
                this.error_phone = false;
            } else {
                this.error_phone_message = '您输入的手机号格式不正确';
                this.error_phone = true;
            }
            if (this.error_phone == false) {
                axios.get(this.host + '/mobiles/'+ this.mobile + '/count/', {
                        responseType: 'json'
                    })
                    .then(response => {
                        if (response.data.count > 0) {
                            this.error_phone_message = '手机号已存在';
                            this.error_phone = true;
                        } else {
                            this.error_phone = false;
                        }
                    })
                    .catch(error => {
                        console.log(error.response.data);
                    })
            }
        },

猜你喜欢

转载自blog.csdn.net/odyssues_lee/article/details/80837025
今日推荐