zoukankan      html  css  js  c++  java
  • 项目中验证用户名+手机号是否存在

    2 接口

    2.1 user/views.py

    class RegCountView(APIView):
        # 注册时需要验证的用户名和手机号是否使用
    
        # 自定义权限类
        permission_classes = (AllowAny,)
        def post(self, request):
            # 接收参数:验证的内容type:username/phone     data:'用户名' 或者:'手机号'
            datatype = request.data.get('type')     # user
            data = request.data.get('data')
            if not all([data, datatype]):
                return Response({'code': '999', 'msg':'参数不完整'})
            if datatype == 'username':
                count = User.objects.filter(username=data).count()
            if datatype == "phone":
                count = User.objects.filter(phone=data).count()
    
            return Response(
                {'code':0, 'msg':'查询成功', 'data': {'type': datatype, 'count':count}}
            )
    

    2.2 user/urls.py

    from django.urls import include, path
    from rest_framework.authtoken.views import obtain_auth_token
    from user import views
    from rest_framework.routers import SimpleRouter, DefaultRouter
    from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
    # 自动生成路由方法, 必须使用视图集
    # router = SimpleRouter() # 没有根路由 /user/ 无法识别
    router = DefaultRouter() # 有根路由
    router.register(r'user', views.UserViewSet)
    urlpatterns = [
        path('index/', views.index),    # 函数视图
        path('login/', obtain_jwt_token), # 获取token,登录视图
        path('refresh/', refresh_jwt_token), # 刷新token
        path('api-auth/', include('rest_framework.urls',namespace='rest_framework')), # 认证地址
        path('register/', views.RegisterView.as_view()), # 注册视图, /user/register/
        path('count/', views.RegCountView.as_view()),		# 验证用户名&手机号是否注册过
    ]
    
  • 相关阅读:
    spring IOC --- 控制反转(依赖注入)----简单的实例
    Spring MVC 返回视图时添加的模型数据------POJO
    Controller接口控制器3
    Controller接口控制器2
    Controller接口控制器
    Spring-MVC:应用上下文webApplicationContext
    DispatcherServlet 前置控制器
    WEB安全 asp+access注入
    WEB安全 Sqlmap 中绕过空格拦截的12个脚本
    Python 爬虫练习(三) 利用百度进行子域名收集
  • 原文地址:https://www.cnblogs.com/msdreamer/p/13941618.html
Copyright © 2011-2022 走看看