Determine whether the user name registration functions to achieve
analysis
Request method: get
url定义:/usernames/(?P<username>\w{5,20})/
Request Parameters: url path parameter
parameter
Types of
Whether the tip must be uploaded
description
username
String
Yes
The user enters a user name
Determine whether the mobile phone number registration function to achieve
analysis
Request Method: GET
url definition:/mobiles/(?P<mobile>1[3-9]\d{9})/
Request Parameters: url path parameter
parameter
Types of
Whether the front must pass
description
mobile
String
Yes
User input phone number
Achieve back-end code
verifications/views.py
from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django_redis import get_redis_connection
from utils.captcha.captcha import captcha #utils/captcha/captcha.py captcha实例
from apps.verifications import constants
import logging
from utils.json_fun import to_json_data
from django.views import View
from apps.users import models
#导入日志器
logger = logging.getLogger('django')
#图形验证
class ImageCode(View):
"""
/verifications/image_codes/
"""
def get(self,request,image_code_id):
# 见captcha.py的最后一行,即输出的元组
text,image = captcha.generate_captcha() #图片展示给用户看,text要临时存入数据库
con_redis = get_redis_connection(alias='verity_codes') #连接指定的redis库
img_key = 'img_{}'.format(image_code_id) #拼接成img_xxx形式
# con_redis.setex(img_key,300,text) #暂存300s
con_redis.setex(img_key,constants.IMAGE_CODE_REDIS_EXPIRES,text) #常量一般单独放置,尽量不改源代码,方便以后修改维护
logger.info('Image_code: {}'.format(text)) #后台日志直接打印,方便调试
return HttpResponse(content=image,content_type='image/jpg')
#用户名验证
# 1.请求方式 不需要修改数据库,只是去数据库查,用GET
# 2.参数 判断用户名是否存在,传递用户名参数 路由传参比较方便 re_path
class CheckUsernameView(View):
"""
Check whether the user exists
GET usernames/(?P<username>|w{5,20})/ #用正则,需要满足这样的方式
"""
def get(self,request,username):
count = models.Users.objects.filter(username=username).count() #统计此用户名的个数
data = {
'count': count,
'username': username,
}
# return JsonResponse({'data': data})
return to_json_data(data=data) #传一个参数,默认传给第一个参数,考虑到位置用关键字传参
#手机号验证
class CheckMobileView(View):
"""
GET mobiles/(?P<mobile>\w{5,20})/
"""
def get(self,request,mobile):
count = models.Users.objects.filter(mobile=mobile).count()
data = {
'count': count,
'mobile': mobile,
}
# return JsonResponse({'data': data})
return to_json_data(data=data)
Creating json_fun.py file utils directory in the project root directory for processing json format conversion function
from django.http import JsonResponse
from .res_code import Code
def to_json_data(errno=Code.OK,errmsg='',data=None,kwargs=None): #data可有可无
json_dict = {'errno':errno, #状态码
'errmsg':errmsg,
'data':data}
#isinstance判断是否是字典 是否存在,是否是字典,是否为空
if kwargs and isinstance(kwargs,dict) and kwargs.keys():
json_dict.update(kwargs) #字典两个合到一起
if kwargs and isinstance(kwargs,dict) and kwargs.keys():
json_dict.update(kwargs)
return JsonResponse(json_dict)
Creating res_code.py file in the root directory of the project utils directory for the situation to return to the front end of the back-end execution