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()), # 验证用户名&手机号是否注册过
]