login_or_not - pai-plznw4me/django-initializer GitHub Wiki
login ์ฌ๋ถ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ page ์ ๋ณด์ฌ์ฃผ๊ณ ์ถ์๋
- views
- html
2๊ฐ์ง ๊ด์ ์์ ์๋์ ๊ฐ์ด ์ฝ๋ฉํ ์ ์๋ค. skeleton ์ฝ๋๋ฅผ ์๋์ ์ ์ด ๋๋ค.
Skeleton
login ์ด ๋๋ฉด authenticate ์์ user ์ ๋ณด๋ฅผ ์ค๋ค.
views.py
from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
# A backend authenticated the credentials
else:
# No backend authenticated the credentials
templates
<!-- templates/home.html-->
{% if user.is_authenticated %}
Hi {{ user.username }}!
{% else %}
<p>You are not logged in</p>
{% endif %}
๋ก๊ทธ์ธ ์ฌ๋ถ checking ํ๊ธฐ
skeleton code 1 : if ๊ตฌ๋ฌธ, is_authenticated ์ฌ์ฉ
- ๋ก๊ทธ์ธ ํ ํ์ ํด๋น function ์ ์ ๊ทผํ๋ฉด ๋ก๊ทธ์ธ ์ฌ๋ถ์ ๋ฐ๋ผ์ ๋ค๋ฅด๊ฒ ์คํ๋จ์ ์์ ์๋ค. reference
def only_login(request):
if request.user.is_authenticated:
return HttpResponse('Login user')
elif request.user.is_authenticated is None:
return HttpResponse('Not login user')
skeleton code : @login_required ์ฌ์ฉ
- ๋ก๊ทธ์ธ ํ ํ์ ํด๋น function ์ ์ ๊ทผํ๋ฉด ๋ก๊ทธ์ธ ์ฌ๋ถ์ ๋ฐ๋ผ์ ๋ค๋ฅด๊ฒ ์คํ๋จ์ ์์ ์๋ค.
@login_required
def only_login(request):
return HttpResponse('Login success!')