custom login required mixins

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

  • 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'])

좋은 웹페이지 즐겨찾기