def login(request): # Creamos el formulario de autenticación vacío form = LoginForm() if request.method == "POST": # Añadimos los datos recibidos al formulario form = LoginForm(data=request.POST) # Si el formulario es válido... if form.is_valid(): # Recuperamos las credenciales validadas username = form.cleaned_data['username'] password = form.cleaned_data['password'] # Verificamos las credenciales del usuario user = authenticate(username=username, password=password) # Si existe un usuario con ese nombre y contraseña if user is not None: # Hacemos el login manualmente do_login(request, user) # Y le redireccionamos a la portada return redirect('/') form.add_error('username','El nombre de usuario o la contraseña no existen') # Si llegamos al final renderizamos el formulario return render(request, "login.html", {'form': form})
def post(self, request): login_form = LoginForm(request.POST) if login_form.is_valid(): # 检查表单提交是否符合规定 email = request.POST.get('email', '') pass_word = request.POST.get('password', '') user = authenticate(username=email, password=pass_word) # 验证是否存在或者对应正确 if user is not None: if user.is_active: login(request, user) # django的auth自带的登录login return redirect('index') else: login_form.add_error(None, error='用户未激活!') return render(request, 'login.html', {'form': login_form}) else: return render(request, 'login.html', {'form': login_form, 'msg': '用户名或密码错误!'}) else: return render(request, 'login.html', {'form': login_form})
def login(request): if request.POST: form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] url_next = form.cleaned_data['next'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: dlogin(request, user) return HttpResponseRedirect(url_next) else: form.add_error(None, "The username and password are not valid.") else: next_url = request.GET.get('next', '/user/') form = LoginForm(initial={'next': next_url}) return render(request, 'accounts/login.html', {'form': form})
def login(request): if request.method == 'POST': login_form = LoginForm(request.POST) value = request.POST.get("code").upper() value2 = request.session.get("verCode", '').upper() if login_form.is_valid(): username = request.POST['username'] password = request.POST['password'] user = auth.authenticate(request, username=username, password=password) if user is not None and value == value2: auth.login(request, user) return redirect(request.GET.get('from', reverse('home'))) elif value != value2: login_form.add_error(None, '验证码不正确') else: login_form.add_error(None, '用户名或密码不正确') else: login_form = LoginForm() context = {} context['login_form'] = login_form return render(request, 'login.html', context)
def login(request): """ 로그인 """ if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): user = User.objects.filter(email=request.POST['email']) if user: password = request.POST['password'] user = user[0] user = authenticate(request, username=user.username, password=password) if user is not None: auth_login(request, user) return redirect('index') else: form.add_error('password', "비밀번호가 일치하지 않습니다.") else: form.add_error('email', "존재하지 않는 계정입니다.") else: form = LoginForm() return render(request, 'user/login.html', {'form': form})