手机号是否存在验证接口
urls.py
router = SimpleRouter()
router.register('', views.LoginView, 'login')
urlpatterns = [
path('', include(router.urls)),
]
views.py
from rest_framework.viewsets import ViewSet
from . import models
import re
class LoginView(ViewSet):
# 校验手机号码是否在数据库中存在接口视图
@action(methods=('GET',), detail=False)
def check_telephone(self, request, *args, **kwargs):
telephone = request.query_params.get('telephone')
if not re.match('^1[3-9][0-9]{9}$', telephone): # 当电话号码格式不正确
return APIResponse(code=0, msg='Error', result='号码格式不正确!')
# 判断数据库中的是否存在当前用户(电话号码)
try:
models.User.objects.get(telephone=telephone)
return APIResponse(code=1, msg='Successful')
except:
return APIResponse(code=0, msg='Error', result='号码不存在')