- 定义路由
from django.conf.urls import url from . import views urlpatterns = [ # 判断用户名是否重复 url(r'^usernames/(?P<username>\w{5,20})/count/$', views.UsernameCountView.as_view()), # 判断手机号是否重复 url(r'^mobiles/(?P<mobile>1[3-9]\d{9})/count/$', views.MobileCountView.as_view()), ]
- 定义接口
- 导入模块
from rest_framework.views import APIView from rest_framework.response import Response from . models import User
- 判断用户号是否存在
class MobileCountView(APIView): """ 判断手机号是否存在 """ def get(self, request, mobile): count = User.objects.filter(mobile=mobile).count() # 响应体 data = { 'mobile': mobile, 'count': count } return Response(data)
- 判断手机号是否存在
class UsernameCountView(APIView): """ 判断用户名是否存在 """ def get(self, requset, username): count = User.objects.filter(username=username).count() # 响应体 data = { 'username': username, 'count': count } return Response(data)
- 导入模块
最一个Django项目(1.5、注册之判断用户名是否存在)
最新推荐文章于 2022-08-08 15:50:43 发布