def login_view(request): """ Log the user in """ # Check if the user is authenticated if request.user.is_authenticated(): # User is authenticated return render(request, "users/login/logged_in.html") else: login_form = LoginForm(request.POST or None) # User is NOT authenticated if request.method == 'POST': # Form data was submitted if login_form.is_valid(): # Form data is valid user = authenticate( username=login_form.cleaned_data['username'], password=login_form.cleaned_data['password']) if user is not None and user.is_active: login(request, user) return render(request, "users/login/logged_in.html") else: # Couldn't authenticate, either the username or password is wrong error = "User doesn't exist or the password is incorrect." login_form._errors['password'] = login_form.error_class( [error]) # Show the login form return render(request, "users/login/login.html", {"form": login_form})
def login_view(request): """ Log the user in """ # Check if the user is authenticated if request.user.is_authenticated(): # User is authenticated return render(request, "users/login/logged_in.html") else: login_form = LoginForm(request.POST or None) # User is NOT authenticated if request.method == 'POST': # Form data was submitted if login_form.is_valid(): # Form data is valid user = authenticate(username = login_form.cleaned_data['username'], password = login_form.cleaned_data['password']) if user is not None and user.is_active: login(request, user) return render(request, "users/login/logged_in.html") else: # Couldn't authenticate, either the username or password is wrong error = "User doesn't exist or the password is incorrect." login_form._errors['password'] = login_form.error_class([error]) # Show the login form return render(request, "users/login/login.html", { "form": login_form })
def login_permalink(request): form = LoginForm() post_auth_action = request.GET.get('post_auth_action', 'redirect'); redirect_to = request.GET.get('redirect_to', None) redirect_to = redirect_to if redirect_to != "/login" else None if request.POST: form = LoginForm(request.POST) if form.is_valid(): email_or_username = form.cleaned_data['username'] password = form.cleaned_data['password'] redirect_to = form.cleaned_data['redirect_to'] post_auth_action = form.cleaned_data['post_auth_action'] if redirect_to == None or redirect_to == 'None': redirect_to = "/" user = attempt_login(request, email_or_username, password) if user is not None and user.is_active: response = HttpResponseRedirect(redirect_to) if post_auth_action == 'close': response = render(request, 'util/closing_window.html') return set_auth_cookies(response, user) else: form._errors["username"] = form.error_class(['The username and password you entered are incorrect.']) return render(request, 'user/login.html', { 'title' : 'Login', 'redirect_to' : redirect_to, 'post_auth_action' : post_auth_action, 'login_form': form})
def login_permalink(request): form = LoginForm() post_auth_action = request.GET.get('post_auth_action', 'redirect') redirect_to = request.GET.get('redirect_to', None) redirect_to = redirect_to if redirect_to != "/login" else None if request.POST: form = LoginForm(request.POST) if form.is_valid(): email_or_username = form.cleaned_data['username'] password = form.cleaned_data['password'] redirect_to = form.cleaned_data['redirect_to'] post_auth_action = form.cleaned_data['post_auth_action'] if redirect_to == None or redirect_to == 'None': redirect_to = "/" user = attempt_login(request, email_or_username, password) if user is not None and user.is_active: response = HttpResponseRedirect(redirect_to) if post_auth_action == 'close': response = render(request, 'util/closing_window.html') return set_auth_cookies(response, user) else: form._errors["username"] = form.error_class( ['The username and password you entered are incorrect.']) return render( request, 'user/login.html', { 'title': 'Login', 'redirect_to': redirect_to, 'post_auth_action': post_auth_action, 'login_form': form })
def login(request): context = RequestContext(request) form = LoginForm() if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = auth.authenticate(username=username, password=password) if user: auth.login(request, user) return redirect('dashboard') else: form._errors['__all__'] = form.error_class( ["Incorrect Username or Password"]) return render_to_response('login.html', {"form": form}, context)