def login_handle(request): uname=request.POST.get('username') upwd=request.POST.get('pwd') remember=request.POST.get('remember') verifycode=request.POST.get('verifycode').strip().lower() user=UserInfo.objects.filter(uname=uname) if verifycode != request.session["validate_code"].lower(): return render(request, 'df_user/login.html', {'error_verifycode':'验证码错误'}) if len(user)!=0: if my_md5(upwd)==user[0].upwd: resp=redirect(reverse('user:user_center_info')) if remember=='1': resp.set_cookie('remember_uname',uname,3600*24*7) else: resp.set_cookie('remember_uname', uname, 3600*0) return resp else: request.session['username'] = uname context = {'title': '用户登录','error_pwd':'密码错误','uname':uname,'upwd':upwd} return render(request, 'df_user/login.html',context) else: context = {'title': '用户登录', 'error_name': '用户名错误','uname': uname, 'upwd': upwd} return render(request, 'df_user/login.html',context)
def register_handle(request): uname = request.POST.get('user_name') upwd = request.POST.get('pwd') ucpwd = request.POST.get('cpwd') uemail = request.POST.get('email') if not 8 <= len(upwd) <= 20: context = {'title': '用户注册', 'error_pwd': '密码长度错误'} return render(request, 'df_user/register.html', context) if (upwd != ucpwd): context = {'title': '用户注册', 'error_cpwd': '密码不一致'} return render(request, 'df_user/register.html', context) try: user = UserInfo.objects.get(uemail=uemail) except UserInfo.DoesNotExist: user = None if user: return render(request, 'df_user/register.html', {'error_email': '邮箱重复'}) #创建对象 UserInfo.objects.create(uname=uname, upwd=my_md5(upwd), uemail=uemail) #注册成功转向登录页面 return redirect(reverse('user:login'))
def reset_handle(request): pwd1=request.POST.get('newpwd1','') pwd2=request.POST.get('newpwd2','') email=request.POST.get('email','') if pwd1!= pwd2: return render(request, 've_user/reset.html', {'msg': '密码不一致!'}) else: user=UserInfo.objects.get(uemail=email) user.upwd=my_md5(pwd2) user.save() return render(request, 'df_user/login.html')