def post(self, request): form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get("login_username") password = form.cleaned_data.get("login_password") authenticated_user = authenticate(username=username, password=password) if authenticated_user and authenticated_user.is_active: django_login(request, authenticated_user) redirect_to = request.GET.get("next", "home_page") return redirect(redirect_to) else: form.add_error(None, "Usuario incorrecto o inactivo") context = {'form': form} return render(request, "login_form.html", context)
def post(self, request): if request.user.is_authenticated(): return redirect('index') postData = request.POST loginForm = LoginForm(postData) if loginForm.is_valid(): username = loginForm.cleaned_data['username'] password = loginForm.cleaned_data['password'] userQuery = User.objects.filter(username__iexact=username) if userQuery.exists(): assert userQuery.count() == 1 username = userQuery[0].username user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) # Everything is OK if 'next' in postData: return redirect(postData['next']) else: return redirect('users_profile', username=username) else: # Account Disabled loginForm.add_error( None, "Your account has been disabled. " "If this is unexpected, please contact the site " "administrators for help.", ) else: # Authentication Failed loginForm.add_error(None, "Login failed!") context = { 'loginForm' : loginForm, } return render(request, self.template_name, context)
def post(self, request): form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('login_username') password = form.cleaned_data.get('login_password') authenticated_user = authenticate(username=username, password=password) if authenticated_user and authenticated_user.is_active: django_login(request, authenticated_user) redirect_to = request.GET.get("next", "home_page") return redirect(redirect_to) else: form.add_error( None, "Usuario incorrecto o inactivo" ) #pinta un error que no pertenece a un field del form #messages.error(request, 'Usuario incorrecto o inactivo') return render(request, 'login_form.html', {'form': form})
def post(self, request): form = LoginForm(request.POST) context = dict() if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") user = authenticate(username=username, password=password) if user is not None: request.session["default-language"] = "es" django_login(request, user) url = request.GET.get('next', '/') return redirect(url) else: form.add_error("__all__", "Wrong username or password") context["form"] = form return render(request, 'login.html', context)
def post(self, request): form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get("login_username") password = form.cleaned_data.get("login_password") # possible_request.POST.get(usesr = User.objects.filter(username=username, password=password) --> no lo puedo hacer asi porque la password hay que pasarla hasheada authenticate_user = authenticate(username=username, password=password) if authenticate_user and authenticate_user.is_active: django_login(request, authenticate_user) redirect_to = request.GET.get( "next", "home_page" ) # si no existe el parametro next devolvemos el segundo parametro en este caso home_page return redirect(redirect_to) else: form.add_error(None, "Usuario incorrecto o inactivo") #messages.error(request, "Usuario incorrecto o inactivo") return render(request, "login_form.html", {'form': form})
def post(self, request, *args, **kwargs): form = LoginForm(request.POST) if not form.is_valid(): return self.render_form(request, form) user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password']) if user is None: form.add_error('password', 'Wrong username or password') return self.render_form(request, form) elif not user.is_active: form.add_error('username', 'User is not active') return self.render_form(request, form) else: login(request, user) return redirect(self.redirect_url(request))
def login(request): if request.user.is_authenticated: return redirect(reverse('index')) if request.method == "POST": login_form = LoginForm(request.POST) if login_form.is_valid(): user = auth.authenticate(username=request.POST['username'], password=request.POST['password']) messages.success(request, "Welcome to 24 hour online conveniance store!") if user: auth.login(user=user, request=request) return redirect(reverse('index')) else: login_form.add_error( None, "Your username or password is incorrect. Please try again") else: login_form = LoginForm() return render(request, 'login.html', {'login_form': login_form})
def post(self, request, *args, **kwargs): form = LoginForm(request.POST) if not form.is_valid(): return self.render_form(request, form) user = authenticate( username = form.cleaned_data['username'], password = form.cleaned_data['password']) if user is None: form.add_error('password', 'Wrong username or password') return self.render_form(request, form) elif not user.is_active: form.add_error('username', 'User is not active') return self.render_form(request, form) else: login(request, user) return redirect(self.redirect_url(request))