Tags
- 통계학
- ORM
- 이진트리
- update
- 완전검색
- create
- 그리디
- regexp
- stack
- 백트래킹
- distinct
- N:1
- 트리
- Article & User
- count
- delete
- Queue
- SQL
- 큐
- 뷰
- migrations
- Django
- Vue
- drf
- 스택
- M:N
- outer join
- 쟝고
- DB
- Tree
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Notice
Recent Posts
Link
데이터 분석 기술 블로그
Django에 대하여(22)_Login 본문
Login
Login은 Session을 Create 하는 과정입니다.
AuthenticationForm()
로그인 인증에 사용할 데이터를 입력 받는 built-in form입니다.
1. 로그인 페이지 작성
# accounts/urls.py
app_name = 'accounts'
urlpatterns = [
path('login/', views.login, name='login'),
]
# accounts/views.py
from django.contrib.auth.forms import AuthenticationForm
def login(request):
if request.method == 'POST':
form = AuthenticationForm(request, request.POST)
# form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
auth_login(request, form.get_user())
return redirect('articles:index')
else:
form = AuthenticationForm()
context = {
'form': form,
}
return render(request, 'accounts/login.html', context)
<!-- accounts/login.html -->
<h1>로그인</h1>
<form actions="{% url 'accounts:login' %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
- login(request, user): AuthenticationForm을 통해 인증된 사용자를 로그인하는 함수입니다.
- get_user(): AuthenticationForm의 인스턴스 메서드로 유효성 검사를 통과했을 경우 로그인 한 사용자 객체를 반환합니다.
2. 세션 데이터 확인하기
로그인 후 발급받은 세션을 django_session 테이블에서 확인합니다.
브라우저에서 확인합니다.
개발자도구 - Application - Cookies
로그인 링크를 작성합니다.
<!-- articles/index.html -->
<h1>Articles</h1>
<a href="{% url 'accounts:login' %}">Login</a>
<a href="{% url 'accounts:create' %}">NEW</a>
<hr>
'백엔드' 카테고리의 다른 글
Django에 대하여(24)_Template with Authentication data (0) | 2024.04.15 |
---|---|
Django에 대하여(23)_Logout (0) | 2024.04.14 |
Django에 대하여(21)_Authentication System (0) | 2024.04.12 |
Django에 대하여(20)_쿠키 (0) | 2024.04.11 |
Django에 대하여(19)_HTTP (0) | 2024.04.10 |