2017-02-17 10 views

答えて

2

使用@login_required機能ベースの景色を望む:

@login_required  
def my_view(request): 
    return HttpResponse('hello') 

ではなくLoginRequiredMixinを使用するために、おそらく簡単ですしかし、あなたは、クラスベースのビュー、

@method_decorator(login_required, name='dispatch') 
class MyView(TemplateView): 
    template_name = 'hello.html' 

    @method_decorator(login_required) 
    def dispatch(self, *args, **kwargs): 
     return super(MyView, self).dispatch(*args, **kwargs) 

@method_decorator(login_required)を使用することができます。

from django.contrib.auth.mixins import LoginRequiredMixin 

class MyView(LoginRequiredMixin, TemplateView): 
    template_name = 'hello.html' 
2

method_decoratorデコレータは、関数デコレータをメソッドデコレータに変換し、メソッドデコレータをインスタンスメソッドで使用できるようにします。

login_decoratorは関数デコレータであるため、ビュー関数でのみ使用できます。

出典:django documentation

関連する問題