Tengo un error al utilizar el decorador @login_required
en una vista basada en clase, pero esta funciona bien en una vista basada en funciones:
Views.py
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from .forms import ElementForm
from .models import Element
@login_required()
class Home(ListView):
model = Element
class ElementDetail(DetailView):
model = Element
@login_required()
def new_element(request):
if request.method == 'POST':
form = ElementForm(request.POST, request.FILES)
if form.is_valid():
element = form.save()
element.save()
return HttpResponseRedirect('/')
else:
form = ElementForm()
template = loader.get_template('new_element.html')
context = {
'form': form
}
return HttpResponse(template.render(context, request))
Urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^elements$', views.Home.as_view(), name='home'),
url(r'^detail/(?P<pk>[0-9]+)/$', views.ElementDetail.as_view(), name='detail'),
url(r'^element/new$', views.new_element, name='new_element'),
]
Al dejar el @login_required
solo en la funcion, el codigo corre sin problemas, pero al agregarlo a la clase class Home, me arroja el siguiente error en consola:
File "/home/didier/Documents/Projects/inventory/inv/elements/urls.py", line 4, in <module>
url(r'^elements$', views.Home.as_view(), name='home'),
AttributeError: 'function' object has no attribute 'as_view'