반응형
로그인/로그아웃
계정관리 위한 앱 설치
$ python manage.py startapp accounts

로그인 기능 추가하기









※ POST 방식은 {% csrf_token %} 보안 설정이 무조건 되어있어야 한다.

from django.shortcuts import render, redirect from django.contrib import auth # login,out을 위해 필수 def login(request) : # request == POST # Login if request.method == "POST" : username = request.POST["username"] password = request.POST["password"] user = auth.authenticate(request, username=username, password=password) # 실제 DB에 있는 회원이라면 로그인 진행 if user is not None : auth.login(request, user) return redirect('home') # 회원이 아니라면 else : return render(request, 'bad_login.html') # request == GET else : return render(request, 'login.html') # login html 띄우기

로그아웃 기능 추가하기



def logout(request) : auth.logout(request) return redirect('home')



화면에 로그인 된 유저들을 띄워보자

<!-- 현재 로그인한 유저라면 유저 네임 띄우기--> {% if user.is_authenticated %} <span class="mr-2 d-none d-lg-inline text-gray-600 small">{{user.username}}</span> {% else %} <span class="mr-2 d-none d-lg-inline text-gray-600 small">Login Plz</span> {% endif %} <img class="img-profile rounded-circle" src="{% static 'img/undraw_profile.svg' %}">

반응형
'○ WEB > 21.02 CodeLion_Django Web' 카테고리의 다른 글
[장고 실전3] 7. 커뮤니티 : 회원가입 (0) | 2021.04.06 |
---|---|
[장고 실전3] 5. 커뮤니티 : 템플릿 가공 (0) | 2021.04.03 |
[장고 실전3] 3. 커뮤니티 : 댓글기능 구현 (0) | 2021.03.30 |
[장고 실전3] 2. 커뮤니티 : 디테일 페이지 (0) | 2021.03.27 |
[장고 실전3] 1. 커뮤니티 : 기초작업, 새글작성하기 (0) | 2021.03.24 |