后台
urls.py
path('mobile/', views.MobileViewSet.as_view({'post': 'check'})),
views.py
# 手机是否存在接口
import re
class MobileViewSet(ViewSet):
def check(self, request, *args, **kwargs):
mobile = request.data.get('mobile', None)
if not mobile:
return APIResponse(1, 'mobile field required')
if not re.match(r'^1[3-9][0-9]{9}$', mobile):
return APIResponse(1, 'mobile field error')
try:
models.User.objects.get(mobile=mobile)
return APIResponse(result=True) # 手机号存在
except:
return APIResponse(result=False) # 手机号不存在