用户注册业务实现
前后端不分离:
1.前后端耦合程度高
2.工作进程相互等待
3.网站搜索排名靠前
4.使用范围有限,适用于web开发
前后端分离:
1.耦合度低
2.适用范围广
重定向里面/index/和index/的区别:
/index/会从根路径开始 http://127.0.0.1:8000/index/
index/会添加在原来的路由后面 http://127.0.0.1:8000/register/index/
1.用户注册
表单——POST方式——后台POST方式提取——验证数据——数据存储
后端代码:
from django.shortcuts import render,redirect
from django.views import View
from django.http import HttpResponse, JsonResponse
import re
from .models import User
from django.contrib.auth import login
from django.db import DatabaseError
from meiduo.utils.response_code.py import RECODE.OK
import logging
logger = logging.getLogger('django')
class RegisterViews(View):
def post(self, request):
'''用户注册'''
# 接收前端数据:username,password,password2,mobile,sms_code,allow
username = request.POST.get('username')
password = request.POST.get('password')
password2 = request.POST.get('password2')
mobile = request.POST.get(',mobile')
sms_code = request.POST.get('sms_code')
allow = request.POST.get('allow') # 单选框勾选是on,没勾选就是None
# 校验数据 all(只要里面有一个为空则返回false)
if all([username,password,password2,mobile,sms_code,allow]) is False:
retunrn HttpResponseForbidden('缺少必传参数')
if not re.match(r'^[a-zA-Z0-9_]{5,20}$', username):
return HttpResponseForbidden('请输入5--20个字符的用户名')
if not re.match(r'^[a-zA-Z0-9]{8,20}$', password):
return HttpResponseForbidden('请输入8-20位密码')
if password2 != password1:
return HttpResponseForbidden('两次密码不一致')
if not re.match(r'^1[3-9]\d{9}$', mobile):
return HttpResponseForbidden('您的手机格式不正确')
# TODO 短信验证码后期补充
#创建user
try:
User.object.create_user(
username = username,
password =password,
mobile = mobile
)
except DatabaseError as e:
logger.error(e)
return render(request,'register',{'register_errmas': '用户注册失败'})
#保持状态
login(request, User) # request.session['id'] = User.id 储存用户的id到session中记录他的登录状态
#重定向到首页
return redirect('/')
新学函数:
校验数据 all(只要里面有一个为空则返回false)
密码存储时需要加密后存储到表中, User.object.create_user自动加密密码
2.用户名是否已经存在
GET请求是速度最快的
pycharm改为ECS6语法
判断是否用户重名urls的配置
程序编写:
from django.shortcuts import render,redirect
from django.views import View
from django.http import HttpResponse,JsonResponse
import re
from .models import User
from django.contrib.auth import login
from django.db import DatabaseError
import logging
from meiduo.utils.response_code.py import RECODE.OK
class UsernameCountVies(View):
'''判断用户名是否注册'''
def get(self, request, username):
# 查询当前用户名的个数,1代表重复
count = User.object.filter(username=username).count()
return JsonResponse({'count': count, 'errmsg': 'OK', 'code':RECODE.OK})
3.手机号是否重复判断
手机号验证url配置
函数编写:
from django.shortcuts import render,redirect
from django.views import View
from django.http import HttpResponse,JsonResponse
import re
from .models import User
from django.contrib.auth import login
from django.db import DatabaseError
import logging
from meiduo.utils.response_code.py import RECODE.OK
class UsernameCountVies(View):
'''判断用户名是否注册'''
def get(self, request, mobile):
# 查询当前用户名的个数,1代表重复
count = User.object.filter(mobile=mobile).count()
return JsonResponse({'count': count, 'errmsg': 'OK', 'code':RECODE.OK})
4.图形验证码
将字符串渲染为图片,作为验证码
利用第三方的包生产图片
单独搞一个子应用来编写图形验证码,短信验证码
url配置:
函数编写:
from django.views import Views
from meiduo.libs.captcha.captha import captcha
from django.http import HttpResponse
from django_redis import get_redis_connection
class ImageCodeView(View):
'''生成图形验证码'''
def get(self, request, uuid)
# 利用SDK生成图形验证码(唯一表示, 图形验证码字符串, 二进制的图片数据)
name, text,image = capcha.generate_captcha()
# 创建redis连接对象
redis_conn = get_redis_connection('verify_code')
# 将图形验证码字符串存入到redis
redis_conn.setex('img_%' % uuid, 300, text)
# 生产的验证码图片响应给前端
return HttpResponse(image, content_type='image/png')
5.短信验证
url配置
函数配置
from django.views import Views
from meiduo.libs.captcha.captha import captcha
from django.http import HttpResponse
from django_redis import redis_conn
class SMSCodeVies(View):
'''短信验证'''
def get(self,request,mobile):
# 接收到前端传入的mobile, image_code, uuid
# 创建redis连接对象, 根据uuid作为key获取到redis中当前用户的图形验证值
# 判断用户写的图形验证码和我们的redis是否一致
# 发送短信
# 将生成好的短信验证码也存储到redis中,以备后期校验
# 响应
pass