def login_view(request): context = build_base_context(request) context['title'] = "Autenticação" if request.user.is_authenticated: if 'next' in request.GET: return HttpResponseRedirect(request.GET['next']) return HttpResponseRedirect( reverse('users:profile', args=[request.user])) if request.method == 'POST': form = f.LoginForm(data=request.POST) if form.is_valid(): user: m.User = form.get_user() login(request, user) # First login (TODO change to some sort of tutorial) if user.last_login is None: return HttpResponseRedirect( reverse('users:profile', args=[user.username])) if 'next' in request.GET: return HttpResponseRedirect(request.GET['next']) return HttpResponseRedirect(reverse('index')) else: context['login_form'] = form else: context['login_form'] = f.LoginForm() return render(request, 'users/login.html', context)
def user_login(request): """ Login page """ user = request.user next_page = request.GET.get('next') next_page = next_page if next_page else reverse('projects:project-index') form = forms.LoginForm() if user.is_authenticated: return redirect(next_page) if request.method == 'POST': form = forms.LoginForm(request.POST) if form.is_valid(): user = form.cleaned_data['user'] auth.login(request, user, backend='django.contrib.auth.backends.ModelBackend') # user is organization member org_pk = Organization.find_by_user(user).pk request.session['organization_pk'] = org_pk request.session.modified = True return redirect(next_page) return render(request, 'users/user_login.html', { 'form': form, 'next': next_page })
def login_view(request): ''' When the LoginForm is submitted and validated the user is authenticated. If they are authenticated then they are logged in and redirected to a view that routes them to their specific profile (Employer/Candidate) ''' if request.method == "POST": form = user_forms.LoginForm(request.POST) if form.is_valid(): user = authenticate( username=form.cleaned_data["email"], password=form.cleaned_data['password'], ) if user: login(request, user) messages.success(request, "You have logged in successfully!") return redirect("logged_user_type") else: messages.warning(request, "Your password/email is incorrect") else: form = user_forms.LoginForm() context = { "form": form, "page_title": "Sign In", } return render(request, "login.html", context)
def delete_user_view(request): ''' Authenticates the user for confirmation to delete their profile ''' if request.method == "POST": form = user_forms.LoginForm(request.POST) if form.is_valid(): user = authenticate( username=form.cleaned_data["email"], password=form.cleaned_data['password'], ) if user: request.user.delete() messages.success(request, "Your account has been deleted") return redirect("jobs") else: messages.warning(request, "Your password/email is incorrect") else: form = user_forms.LoginForm() context = { "form": form, "page_title": "Delete Account", } return render(request, "delete.html", context)
def member_or_guest_login(request): """ 会員・ゲスト確認ビュー """ # 会員がログイン中の場合すぐチェックアウト画面に移動する if request.user.is_authenticated: return redirect("orders:checkout") # リクエストがPOSTの場合 if request.method == "POST": # 会員ログインを選択した場合の処理 if "member_login" in request.POST: member_form = user_forms.LoginForm(request.POST, prefix="member") if member_form.is_valid(): member_email = member_form.cleaned_data.get("email") member_password = member_form.cleaned_data.get("password") # ログイン処理を行う user = authenticate(request, email=member_email, password=member_password) if user is not None: login(request, user) # 既に作成したデザインをユーザに登録する try: design = design_models.Design.objects.get( pk=request.session["design"]) design.user = user design.save() except: pass return redirect(reverse("orders:checkout")) guest_form = forms.GuestForm(prefix="guest") # ゲストログインの場合の処理 elif "guest_login" in request.POST: guest_form = forms.GuestForm(request.POST, prefix="guest") if guest_form.is_valid(): guest_email = guest_form.cleaned_data.get("email") try: user_models.Guest.objects.get(email=guest_email) except user_models.Guest.DoesNotExist: user_models.Guest.objects.create(email=guest_email) request.session["guest_email"] = guest_email return redirect(reverse("orders:checkout")) member_form = user_forms.LoginForm(prefix="member") # リクエストがPOST以外の場合 else: guest_form = forms.GuestForm(prefix="guest") member_form = user_forms.LoginForm(prefix="member") context = {"member_form": member_form, "guest_form": guest_form} return render(request, "orders/member-or-guest.html", context)
def post(self, request): if not request.user.is_authenticated: login_form = users_forms.LoginForm(request.POST) if login_form.is_valid(): user_name = request.POST.get("username", "") pass_word = request.POST.get("password", "") user_name = user_name.strip() pass_word = pass_word.strip() user = modelhelp.authenticate(username=user_name, password=pass_word) if user is not None: if user.is_active: login(request, user) user = json.dumps(modelhelp.ajaxuser(user)) response = HttpResponse('{"status":"success","msg":"登录成功","data":%s}' % user, content_type='application/json') return modelhelp.add_auth_response(response) else: response = HttpResponse('{"status":"fail","msg":"账户未激活"}', content_type='application/json') else: response = HttpResponse('{"status":"fail","msg":"用户名或密码错误"}', content_type='application/json') else: response = HttpResponse(json.dumps(login_form.errors), content_type='application/json') return modelhelp.del_auth_response(response) user = json.dumps(modelhelp.ajaxuser(request.user)) response = HttpResponse('{"status":"success","msg":"用户是登录状态","data":%s}'%user, content_type='application/json') return modelhelp.add_auth_response(response)
def user_login(request): LogForm = forms.LoginForm(request.POST or None) print("User logged in: " + str(request.user.is_authenticated)) if LogForm.is_valid(): username = LogForm.cleaned_data.get("uname") password = LogForm.cleaned_data.get("password") auth_user = authenticate(username=username, password=password) if auth_user is not None: login(request, auth_user) messages.info(request, f"You are logged in as: {username}") return redirect("../") return render(request, 'users/login.html', {'form_dic': LogForm})
def auth_forms(request): if not request.user.is_authenticated: return { 'auth_forms': { 'login_form': forms.LoginForm(), 'login_url': reverse_lazy('users:login'), 'register_form': forms.RegisterForm(), 'register_url': reverse_lazy('users:register'), } } else: return {}
def login(request): form = forms.LoginForm(data=(request.POST or None)) if request.method == 'POST': # TODO: Handle inactive users if form.is_valid(): auth_login(request, form.get_user()) # Set session to not expire on browser close if form.cleaned_data['remember_me']: request.session.set_expiry(settings.SESSION_REMEMBER_DURATION) return redirect('my_badges') return home(request, login_form=form)
def login(request): redirect_to = request.POST.get('next',request.GET.get('next', '')) if request.method=='POST': form=forms.LoginForm(request.POST) if form.is_valid(): login_name=form.cleaned_data['username'] login_password=form.cleaned_data['password'] user=authenticate(username=login_name,password=login_password) if user is not None: if user.is_active: auth.login(request,user) if redirect_to: return redirect(redirect_to) else: return redirect('/') else: messages.add_message(request,messages.WARNING,'账户未启用!') else: messages.add_message(request,messages.WARNING,'账号或密码错误!') else: messages.add_message(request, messages.WARNING, '账号或密码错误!') else: form = forms.LoginForm() return render(request,'users/login.html',context={'form':form,'next': redirect_to})
def post(self, request): login_form = forms.LoginForm(request.POST) if login_form.is_valid(): user_name = request.POST.get("username", "") user_pawd = request.POST.get("password", "") user = authenticate(username=user_name, password=user_pawd) if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect(reverse('article:article_list')) else: return render(request, "login.html", {"msg": "用户名或密码错误!"}) else: return render(request, "login.html", {"msg": "用户名或密码错误!"}) else: return render(request, "login.html", {"msg": "用户名或密码错误!", "login_form": login_form})
def post(self, request): form = forms.LoginForm(request.POST) not_verified = None if form.is_valid(): email = form.cleaned_data.get("email") password = form.cleaned_data.get("password") user = authenticate(request, username=email, password=password) if user is not None: check_user = users_models.User.objects.get(email=email) pk = check_user.pk if check_user.email_verified is True: login(request, user) return redirect(reverse("core:home")) else: not_verified = "이메일 인증을 해주세요." return render(request, "users/login.html", { "form": form, "not_verified": not_verified, })
def login_page(request): # login form form = users_form.LoginForm(auto_id=True) context = {'form': form} return TemplateResponse(request, 'users/login.html', context=context)
def get(self, request): form = forms.LoginForm() return render(request, "users/login.html", {"form": form})
def _form(self, email, password): return forms.LoginForm(data={'username': email, 'password': password})