From 69c51b4e205d859d8bcd0a383e1c3c58e81c15ec Mon Sep 17 00:00:00 2001 From: nohkwanok Date: Tue, 12 Mar 2024 15:19:49 +0900 Subject: [PATCH] implement keep context if user move next page after login/signup --- web/project/app/views.py | 2 +- web/project/auth/views.py | 27 ++++++++++++++++++------- web/project/templates/auth/signin.html | 4 ++-- web/project/templates/auth/signup.html | 4 ++-- web/project/templates/base/base.html | 4 ++-- web/project/templates/recommend/ai.html | 20 ++++++++++++++++-- 6 files changed, 45 insertions(+), 16 deletions(-) diff --git a/web/project/app/views.py b/web/project/app/views.py index a2457bc..6a1a148 100644 --- a/web/project/app/views.py +++ b/web/project/app/views.py @@ -64,4 +64,4 @@ def get_account_by_summoner_name(request: WSGIRequest): def recommend_ai(request: WSGIRequest): - return render(request, "recommend/ai.html") + return render(request, "recommend/ai.html", {"user": request.user}) diff --git a/web/project/auth/views.py b/web/project/auth/views.py index 205bc15..c1f9a12 100644 --- a/web/project/auth/views.py +++ b/web/project/auth/views.py @@ -1,11 +1,16 @@ from django.contrib.auth import authenticate, login, logout -from django.shortcuts import render, redirect +from django.shortcuts import render, redirect, reverse from .forms import SigninForm, SignupForm def signin(request): + _next = request.GET.get("next") + if request.user.is_authenticated: - return redirect("home") + if _next: + return redirect(_next) + else: + return redirect("home") if request.method == "POST": form = SigninForm(request.POST) @@ -15,11 +20,14 @@ def signin(request): user = authenticate(request, username=username, password=password) if user is not None: login(request, user) - return redirect("home") + if _next: + return redirect(_next) + else: + return redirect("home") else: form.add_error(None, "Invalid username or password") - return render(request, "auth/signin.html") + return render(request, "auth/signin.html", {"next": _next}) def signout(request): @@ -29,13 +37,18 @@ def signout(request): def signup(request): + _next = request.GET.get("next") + if request.user.is_authenticated: - return redirect("home") + if _next: + return redirect(_next) + else: + return redirect("home") if request.method == "POST": form = SignupForm(request.POST) if form.is_valid(): form.save() - return redirect("auth:signin") + return redirect(reverse("auth:signin") + f"?next={_next}") - return render(request, "auth/signup.html") + return render(request, "auth/signup.html", {"next": _next}) diff --git a/web/project/templates/auth/signin.html b/web/project/templates/auth/signin.html index c68eebf..213ee64 100644 --- a/web/project/templates/auth/signin.html +++ b/web/project/templates/auth/signin.html @@ -9,7 +9,7 @@

LOGIN최고의 듀오를 추천드릴게요 🔥

-
+ {{ csrf_input }}
@@ -46,7 +46,7 @@

회원이 아니신가요? 무료로 가입해보세요. - 회원가입

diff --git a/web/project/templates/auth/signup.html b/web/project/templates/auth/signup.html index 75e90f1..3410dc8 100644 --- a/web/project/templates/auth/signup.html +++ b/web/project/templates/auth/signup.html @@ -9,7 +9,7 @@

REGISTE

반가워요 😊

- + {{ csrf_input }}
@@ -66,7 +66,7 @@

이미 회원이신가요? - 로그인

diff --git a/web/project/templates/base/base.html b/web/project/templates/base/base.html index 5321f5b..fda70e5 100644 --- a/web/project/templates/base/base.html +++ b/web/project/templates/base/base.html @@ -103,11 +103,11 @@ } function signin() { - location.href = "{{ url('auth:signin') }}"; + location.href = "{{ url('auth:signin') }}?next={{ request.path }}"; } function signup() { - location.href = "{{ url('auth:signup') }}"; + location.href = "{{ url('auth:signup') }}?next={{ request.path }}"; } function signout() { diff --git a/web/project/templates/recommend/ai.html b/web/project/templates/recommend/ai.html index f265866..9da6eb0 100644 --- a/web/project/templates/recommend/ai.html +++ b/web/project/templates/recommend/ai.html @@ -3,7 +3,23 @@ | AI Recommend {% endblock title %} {% block content %} -
- Hello +
+ {% if not user.is_authenticated %} +

안녕하세요, 방문자님

+

+ 로그인을 + 하시면 AI 추천을 받아보실 수 있습니다. +

+ {% elif not user.summoner_info %} +

안녕하세요, {{ user.username }}님

+

+ 라이엇 계정 연동을 통해 AI 추천을 받아보실 수 있습니다. +

+ {% else %} +

안녕하세요, {{ user.username }}님

+ + {% endif %}
{% endblock content %}