def login(request): if request.method == 'GET': form = LoginForm() return render(request,'login.html',{'form':form}) else: form = LoginForm(request.POST) if form.is_valid(): # form.cleaned_data#{"username":"******",'password':'******'} form.cleaned_data['password'] = md5(form.cleaned_data['password']) # user=models.UserInfo.objects.filter(**form.cleaned_data).first() user = UserInfo.objects.filter(**form.cleaned_data).first() print(user,"user") if user: ###将用户信息方session # permissions_list = user.role. # print(permissions_list, "###################") #request.session[settings.USER_SESSION_KEY] ={'id':user.pk,'username':user.username} # permissions_list= user.role.fiter(permission__id__isnull=False).values().distinct() # 当前用户的所有权限 init_permissions(user,request) return redirect('/user/') else: form.add_error("password","用户名或密码错误") return render(request,'login.html',{'form':form})
def login(request): if request.method == 'GET': form = LoginForm() return render(request, 'login.html', {'form': form}) else: form = LoginForm(request.POST) if form.is_valid(): # form.cleaned_data#{"username":"******",'password':'******'} form.cleaned_data['password'] = md5(form.cleaned_data['password']) user = UserInfo.objects.filter(**form.cleaned_data).first() # print(user,"登录用户") if user: ###将用户信息放session init_permissions(user, request) return redirect('/index/') else: form.add_error("password", "用户名或密码错误") return render(request, 'login.html', {'form': form})
def login(request): if request.method == "GET": form = LoginForm() return render(request,'login.html',{'form':form}) else: form = LoginForm(request.POST) if form.is_valid(): # form.cleaned_data # {'username':'******','password':'******'} # models.UserInfo.objects.filter(username=form.cleaned_data['user'],password=form.cleaned_data['pwd']) # models.UserInfo.objects.filter(**{'username':'******','password':123}) form.cleaned_data['password'] = md5(form.cleaned_data['password']) user = models.UserInfo.objects.filter(**form.cleaned_data).first() if user: # 将用户信息放置到session中 request.session[settings.USER_SESSION_KEY] = {'id':user.pk,'username':user.username } return redirect('/index/') else: form.add_error('password', '用户名或密码错误') return render(request, 'login.html',{'form':form})
def register1(request): if request.method == "GET": roles = Role.objects.all() form = LoginForm() return render(request,'reg.html',{'form':form,"roles":roles}) else: form = LoginForm(request.POST) print(form) if form.is_valid(): # form.cleaned_data # {'username':'******','password':'******'} # models.UserInfo.objects.filter(username=form.cleaned_data['user'],password=form.cleaned_data['pwd']) # models.UserInfo.objects.filter(**{'username':'******','password':123}) # form.cleaned_data['password'] = md5(form.cleaned_data['password']) user = User.objects.filter(**form.cleaned_data).first() print(">>>>",user) if user: # 将用户信息放置到session中 request.session[settings.USER_SESSION_KEY] = {'id':user.pk,'username':user.username } return redirect('/login/') else: form.add_error('password', '用户名或密码错误') return render(request, 'reg.html',{'form':form})
def login(request): if request.method == "GET": form = LoginForm() return render(request, "login.html", {"form": form}) else: # 将接收到的数据,传给LoginForm()类 通过form 验证; form = LoginForm(request.POST) # 通过 form 验证后,form 为True if form.is_valid(): # 通过form验证的数据 存放在 cleaned_data 中 # form.cleaned_data # 通过自定义 md5 模块验证密码 form.cleaned_data["password"] = md5(form.cleaned_data["password"]) # 通过 **form.cleaned_data 可以将 数据依照字典形式获取 filter({"username":zhangsan,"password":1234}) userinfo = models.UserInfo.objects.filter( **form.cleaned_data).first() # 拿取第一个对象值 # 如果userinfo 中有数据,即表示验证成功 if userinfo: # 将 用户信息 放置到 session 中 request.session[settings.USER_SESSION_KEY] = { "id": userinfo.pk, "username": userinfo.username } # 重定向 页面 return redirect("/index/") else: # 用户验证失败 form.add_error("password", "用户名或密码错误") # 存放错误信息 form.errors # 将 form 清洗过的 数据直接返回给 html 模板 return render(request, "login.html", {"form": form})