Ross Symonds
Ross Symonds

Reputation: 710

object has no attribute 'object_list'

I am getting an error and I think it may be for the same reasons Viktor had a problem ('ProductList' object has no attribute 'object_list')

On Viktors post AKS wrote -

You might be getting the error on following line in get_context_data() of the super class:

queryset = kwargs.pop('object_list', self.object_list)

The get method of BaseListView sets the object_list on the view by calling the get_queryset method:

self.object_list = self.get_queryset()

But, in your case, you are calling get_context_data() in get_queryset method itself and at that time object_list is not set on the view.

I reviewed my trace back and saw code that AKS had mentioned.

  File "C:\Users\HP\Django Projects\EcommerceProject\products\views.py", line 48, in get_context_data
    context = super(ProductListView, self).get_context_data(*args, **kwargs)
  File "c:\Users\HP\DJANGO~1\ECOMME~1\lib\site-packages\django\views\generic\list.py", line 131, in get_context_data
    queryset = kwargs.pop('object_list', self.object_list)
AttributeError: 'ProductListView' object has no attribute 'object_list'
[03/Aug/2020 18:20:21] "GET /products/ HTTP/1.1" 500 90906

Here is my code

class ProductListView(ListView):
    model = Product
    template_name = "products/list.html"
    # added for pagination
    context_object_name='object_list' #Default: object_list
    # print(context_object_name)
    # paginate_by = 3

def get_context_data(self, *args, **kwargs):
    context = super(ProductListView, self).get_context_data(*args, **kwargs)
    cart_obj, new_obj = Cart.objects.new_or_get(self.request)
    print("get context data")
    # context = {}
    context['cart'] = cart_obj
    return context



def get_queryset(self, *args, **kwargs):
    request = self.request
    return Product.objects.all()

def get(self, request):
    
    paginate_by = request.GET.get('paginate_by',6) or 4
    data = self.model.objects.all()
    paginator = Paginator(data, paginate_by)
    page = request.GET.get('page')
   
    try:
        paginated = paginator.page(page)
    except PageNotAnInteger:
        paginated = paginator.page(1)
    except EmptyPage:
        paginated = paginator.page(paginator.num_pages)

    # context={}
    context = self.get_context_data()
    context['DataPaginated'] = paginated
    context['paginate_by'] = paginate_by

    # return render(request, self.template_name, {'DataPaginated':paginated, 'paginate_by':paginate_by, 'cart':cart_obj})
    return render(request, self.template_name, context)

Issue Fixed

I slightly modified the get_context_data function in the class ProductListView(ListView):

def get_context_data(self, *args, **kwargs):
    self.object_list = super().get_queryset()
    context = super(ProductListView, self).get_context_data(*args, **kwargs)
    

Upvotes: 3

Views: 1897

Answers (1)

jrsenthilk
jrsenthilk

Reputation: 9

What is the exact issue are you trying to solve or what exactly are you trying to solve through the code?

Do you want to set a context object name or do you want to retrieve data from the Product model?

If retrieve data from Product model, you can just use the objects.filter.

If this line : context = super(ProductListView, self).get_context_data(*args, **kwargs) is the issue you are trying to solve.. try moving the def get_context_data to be within the ProductListView class.

Upvotes: 0

Related Questions