# django中认证模型系统有一个login函数,封装了写入session操作可以帮助我们登录用户,并且实现状态保持
login(request,yonghu)
后端效验
class ZhuCeView(View):
# get方式打开注册页面
def get(self, request):
return render(request, "users/zhuce.html")
# 注册接口
def post(self, request):
# 1. 获取传递到后台的请求参数
username = request.POST.get("username")
password = request.POST.get("password")
password2 = request.POST.get("password2")
mobile = request.POST.get("mobile")
allow = request.POST.get("allow")
# 校验参数
if not all([username, password, password2, mobile, allow]):
return HttpResponseForbidden("缺少必填参数")
if not re.match(r'^[a-zA-Z0-9_-]{5,18}$', username):
return HttpResponseForbidden("请输入5-18位用户名")
if not re.match(r'^[0-9A-Za-z]{6,16}$', password):
return HttpResponseForbidden("请输入6-16位的密码")
if password != password2:
return HttpResponseForbidden("两次密码输入不一致")
if not re.match(r'^1[3-9]\d{9}$', mobile):
return HttpResponseForbidden("请输入正确格式的手机号")
if allow != 'on':
return HttpResponseForbidden("请勾选用户协议")
# 3. 注册添加用户
try:
yonghu = MyUser.objects.create_user(username=username, password=password, mobile=mobile)
except Exception:
# 插入/注册失败
return render(request, 'users/zhuce.html', context={"zhuceshibai": "注册失败"})
# 4. 注册登陆成功-->需要状态保持
# django中认证模型系统有一个login函数,封装了写入session操作可以帮助我们登录用户,并且实现状态保持
login(request,yonghu)
# 5. 重定向到首页
return redirect(reverse("shouye:index"))
新建首页子应用
# 1. 新建首页子应用
python manage.py startapp shouye
# 2. 主应用settings.py注册子应用app
# 3. 主应用urls.py绑定子应用路由(子应用新建urls.py)
re_path(r'^', include(('shouye.urls', "shouye"), namespace="shouye")),
# 4. 首页子应用创建视图函数
class ShouYeView(View):
def get(self,request):
return render(request,"shouye/index.html")
# 5. 子应用路由绑定视图函数
re_path(r'index/$', ShouYeView.as_view(), name="index"),
标签:username,return,get,后端,request,效验,HttpResponseForbidden,password,05
From: https://www.cnblogs.com/kh-1314/p/17100756.html