@login_required in view-based classes

1

You see, I have the following view:

class contenido_adicional(View):
    def get(self,request):
        form=bonus()
        return render(request,'privilegio.html',{'forma':form})
    def post(self,request):
        form=bonus(request.POST)
        if form.is_valid():
            tex=form.cleaned_data['texto']
            ani=form.cleaned_data['animal']
            documento=extra(texto=tex,animal=ani)
            documento.save()
            return HttpResponseRedirect('/')
        return render(request,'privilegio.html',{'forma':form})

I want to make that view can only be accessed by a user with an open session. I know that if I want a request type view that does not enter a user without a session, I can use @login_required, but this does not work in view views.

    
asked by Miguel Alparez 29.05.2017 в 18:57
source

1 answer

2

You have to make your view extend to LoginRequiredMixin

from django.contrib.auth.mixins import LoginRequiredMixin

class contenido_adicional(LoginRequiredMixin, View):
    def get(self,request):
        form=bonus()
        return render(request,'privilegio.html',{'forma':form})
    def post(self,request):
        form=bonus(request.POST)
        if form.is_valid():
            tex=form.cleaned_data['texto']
            ani=form.cleaned_data['animal']
            documento=extra(texto=tex,animal=ani)
            documento.save()
            return HttpResponseRedirect('/')
        return render(request,'privilegio.html',{'forma':form})
    
answered by 30.05.2017 / 13:46
source