I am trying to create a list_view for a model queryset. When running my server, it returns : attribute error - 'function' object has no attribute 'as_view'. I would appreciate helping me in solve this.
Here's my code:
Views.py:
@login_required
class live_bids(ListView):
model = Post
template_name = 'loggedin_load/live_bids.html'
def get_queryset(self):
return Post.objects.all().prefetch_related('bids').filter(user=self.request.user)
urls.py:
url(r'^live_bids/$', live_bids.as_view()),
You can't use the
login_required
decorator on a class like that. You need to usemethod_decorator
. On Django 1.9+, you can decorate the class:On earlier versions, you need to override
dispatch
and usemethod_decorator
there.The easiest solution is to use
LoginRequiredMixin
instead of the decorator (works with Django 1.9+)Note that in the examples, I have renamed the view to
LiveBids
, to match the recommended Django style. You'll have to update the url pattern as well.