def login(request): print(request.method) if request.method == "GET": try: user = request.session.get("user", None) print(user) except AttributeError: return render(request, "login.html") else: if user: return render(request, "home.html") return render(request, "login.html") if request.method == "POST": request.encoding = "utf-8" login_info = request.POST f = LoginForm(login_info) if f.is_valid(): name = f.cleaned_data["username"] pwd = f.cleaned_data["userpass"] remember = f.cleaned_data["remember"] res_set = User.objects.filter(name=name) if len(res_set) == 0: return render(request, "login.html", {"error_msg": "用户名或者密码错误,请重新输入!"}) user = res_set[0] if str(user.password) == pwd: request.session[SESSION_USER_NAME] = user.name if remember: pass # TODO 需要添加记住我功能 return redirect(init_home) elif str(user.password) != pwd: return render(request, "login.html", {"error_msg": "用户名或者密码错误,请重新输入!"}) return render(request, "login.html", {"obj": f.errors})
def test_login_username_false(self): """ Check if username is already exists. Do not valid the form. """ form = LoginForm(data={ "username": "******", "password": "******" }) self.assertFalse(form.is_valid())
def test_login_form_is_valid(self): """ Check if login form has good value. Valid the form. """ form = LoginForm(data={ "username": "******", "password": "******" }) self.assertTrue(form.is_valid())
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') user = User.objects.get(username=username) res = HttpResponseRedirect(reverse('goods:index')) request.session['user_id'] = user.id request.session.set_expiry(86400) return res errors = form.errors return render(request, 'login.html', {'errors': errors})
def login(request): if request.method == 'GET': return render(request,'login.html') if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): # 密码相同 username = form.cleaned_data['username'] user = User.objects.filter(username=username).first() request.session['user_id'] = user.id return HttpResponseRedirect(reverse('goods:index')) else: errors = form.errors return render(request,'login.html',{'errors':errors})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': # 使用表单校验post传递的参数 form = LoginForm(request.POST) if form.is_valid(): # 如果成功 user = auth.authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) auth.login(request, user) return render(request, 'index.html') errors = form.errors return render(request, 'login.html', {'errors': errors})
def post(self, request): login_form = LoginForm(request.POST) if login_form.is_valid(): username = request.POST.get('username', '') password = request.POST.get('password', '') user = authenticate(username=username, password=password) if user: if user.is_active: login(request, user) return HttpResponseRedirect(reverse('report:report_list')) else: return render(request, 'user/login.html', {'msg': '该用户未激活'}) else: return render(request, 'user/login.html', {'msg': '用户名活着密码错误'}) else: return render(request, 'user/login.html', {'login_form': login_form})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = User.objects.get(username=username) # 校验密码是否一致 if not check_password(password, user.password): pwd_error = '密码错误' return render(request, 'login.html', {'pwd_error': pwd_error}) res = HttpResponseRedirect(reverse('goods:index')) # session中纯属user_id request.session['user_id'] = user.id request.session.set_expiry(86400) return res errors = form.errors return render(request, 'login.html', {'errors': errors})