์ง๋ ์๊ฐ์ ๊ธฐ๋ณธ์ ์ธ ์ฅ๊ณ ์ ์ฌ์ฉ์ ๋ชจ๋ธ์ ๋ฐ๊ฟ์
AUTH_USER_MODEL์ user.UserModel ํด๋์ค๋ฅผ ๋ฃ์ด์คฌ๊ธฐ ๋๋ฌธ์
์ด ๋ฐ๋ ๋ด์ฉ์ ์ ์ ์์ฑํ๋ ํ์๊ฐ์ , ๋ก๊ทธ์ธ ๋ชจ๋ธ์์๋ ๋ชจ๋ ๋ฐ๊ฟ์ค์ผ ํ๋ค!
user ์ฑ์ views.py์ ์๊ธฐ ๋๋ฌธ์ ํ์ผ ์์ ํด์ฃผ์!
ํ์๊ฐ์ ์์ ํ๊ธฐ(sign_up_view)
user ์ฑ์ views.py์ sign_up_view ํจ์๋ฅผ ์์ ํด๋ณด์!
๋จผ์ ๋งจ ์๋จ์ ์๋ ์ฝ๋๋ฅผ ์ ๋ ฅํ์
# user/views.py
from django.contrib.auth import get_user_model #์ฌ์ฉ์๊ฐ ์๋์ง ๊ฒ์ฌํ๋ ํจ์
post๋ฅผ ์์ ํด์ผ ํ๋ค!
# user/views.py
def sign_up_view(request):
if request.method == 'GET':
return render(request, 'user/signup.html')
elif request.method == 'POST':
username = request.POST.get('username', None)
password = request.POST.get('password', None)
password2 = request.POST.get('password2', None)
bio = request.POST.get('bio', None)
if password != password2:
return render(request, 'user/signup.html')
else:
exist_user = get_user_model().objects.filter(username=username)
if exist_user:
return render(request, 'user/signup.html') # ์ฌ์ฉ์๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์๋ฅผ ์ ์ฅํ์ง ์๊ณ ํ์๊ฐ์
ํ์ด์ง๋ฅผ ๋ค์ ๋์
else:
UserModel.objects.create_user(username=username, password=password, bio=bio)
return redirect('/sign-in') # ํ์๊ฐ์
์ด ์๋ฃ๋์์ผ๋ฏ๋ก ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ด๋
์๋ฒ ๋๋ ค์ ํ์๊ฐ์ ํ๋๊น
์ ๊ธฐํ๊ฒ ์์ฃผ ์ ๋์จ๋ค!! ์ด์ ๋ก๊ทธ์ธ์ ์์ ํด๋ณด์!
๋ก๊ทธ์ธ ์์ ํ๊ธฐ(sign_in_view)
sign_up ๊ณผ ๋์ผํ๊ฒ user ์ฑ์ views.py์ sign_up_view๋ฅผ ์์ ํด์ฃผ์!
๋จผ์ ์๋์ ์ฝ๋๋ฅผ ์ ๋ ฅํด์ฃผ์!
์ฅ๊ณ ๊ฐ ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํ๋ ๋ชจ๋ ์ถ๊ฐํด์ฃผ๋ ๊ฒ!
# user/views.py
from django.contrib import auth # ์ฌ์ฉ์ auth ๊ธฐ๋ฅ
์๋์ ์ฝ๋๋ก ์์ ํด์ผํ๋ค
์ฌ๊ธฐ์ authenticate๋ผ๋ ๊ธฐ๋ฅ์ ์ํธํ๋ ๋น๋ฐ๋ฒํธ์ ์ ๋ ฅํ ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ๋์ง ์ฌ์ฉ์์ ๋ง๋์ง
ํ๋ฒ์ ํ์ธ์์ผ์ค๋ค!!
me = auth.authenticate(request, username=username, password=password)
๊ทธ๋ฆฌ๊ณ ์ฌ์ฉ์๊ฐ ์๋์ง ์๋์ง๋ง ๊ตฌ๋ถํด์ฃผ๋ฉด ๋๊ธฐ๋๋ฌธ์ if ๋ฌธ๋ ์์ ํด์ค๋ค
def sign_in_view(request):
if request.method == 'POST':
username = request.POST.get('username', None)
password = request.POST.get('password', None)
me = auth.authenticate(request, username=username, password=password)
if me is not None: # ์ ์ฅ๋ ์ฌ์ฉ์์ ํจ์ค์๋์ ์
๋ ฅ๋ฐ์ ํจ์ค์๋ ๋น๊ต
auth.login(request, me)
return HttpResponse("๋ก๊ทธ์ธ ์ฑ๊ณต")
else:
return redirect('/sign-in') # ๋ก๊ทธ์ธ ์คํจ
elif request.method == 'GET':
return render(request, 'user/signin.html')
'๐๐ช > Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
3์ฃผ์ฐจ_4๋ฒ์งธ_๋ก๊ทธ์ธ ํ์ ๊ธฐ๋ฅ, ๋ก๊ทธ์์ ๋ง๋ค๊ธฐ (1) | 2022.09.23 |
---|---|
3์ฃผ์ฐจ_3๋ฒ์งธ_๋ก๊ทธ์ธ ์ดํ ๊ธฐ๋ฅ ๋ค๋ฌ๊ธฐ (0) | 2022.09.23 |
3์ฃผ์ฐจ_1๋ฒ์งธ_์ฌ์ฉ์ ๋ชจ๋ธ ๋น๊ต, ๋ชจ๋ธ ์ ๊ทธ๋ ์ด๋, ์ ์ฉ (0) | 2022.09.23 |
2์ฃผ์ฐจ_5๋ฒ์งธ_๋ก๊ทธ์ธ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ (1) | 2022.09.23 |
2์ฃผ์ฐจ_4๋ฒ์งธ_ํ์๊ฐ์ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ (0) | 2022.09.23 |
๋๊ธ