custom login required mixins

GisangLee·2022년 3월 21일
0

django

목록 보기
8/35

로그인된 사용자만 이용가능

  • login_require.py
from django.shortcuts import redirect

def login_required(fn):

    def wrapper_fn(request, *args, **kwargs):
        admin_id = request.session.get('admin', None)
        if admin_id is None:
            return redirect("http://127.0.0.1:8000")
        return fn(request, *args, **kwargs)

    return wrapper_fn
  • my_view.py
@method_decorator(login_required, name="dispatch")
class HomeView(View):
    def get(self, request, *args, **kwargs):
  • login view ( post, delete 요청 > 로그인, 로그아웃)
def post(self, request):
...
..
.
	request.session["admin"] = admin_id	
    
def delete(self, request):
	if request.session.get('admin'):
		del(request.session['admin'])
profile
포폴 및 이력서 : https://gisanglee.github.io/web-porfolio/

0개의 댓글