Djangoの特定Viewに認証認可login_requiredする方法

Function BasedとClass Basedともにデコレーターを使用する。

目次

Function Based Viewの場合

views.py

from django.contrib.auth.decorators import login_required

@login_required
def index(request):
    pass

Class Based Viewの場合

views.py

from django.views.generic import TemplateView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

@method_decorator(login_required, name='dispatch')
class IndexView(TemplateView):
    pass

Class Based Viewには他の方法もある。

views.py

from django.contrib.auth.mixins import LoginRequiredMixin

class IndexView(TemplateView,LoginRequiredMixin):
    pass
よかったらシェアしてね!
  • URLをコピーしました!
目次