对象没有属性“ object_list” [英] object has no attribute 'object_list'

查看:184
本文介绍了对象没有属性“ object_list”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误,我认为可能是出于同样的原因,Viktor遇到了问题( ProductList对象没有属性 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')

在Viktors上AKS写道-

On Viktors post AKS wrote -

在超类的get_context_data()中的下一行得到错误:

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)

BaseListView 的get方法设置 get_queryset 方法在视图上> 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()

但是,在您的情况下,您正在调用 get_queryset 方法本身中的get_context_data()以及当时未在 object_list 上设置视图。

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.

我回顾了我的踪迹,看到了AKS提到的代码。

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

这是我的代码

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)

问题已解决

我在类 get_context_data 函数> ProductListView(ListView):

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)
    


推荐答案

您要解决的确切问题是什么?通过代码?

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?

如果检索数据从产品模型中,您可以只使用objects.filter。

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

如果此行: context = super(ProductListView,self).get_context_data(* args,** kwargs)是您要解决的问题。.尝试将 def get_context_data 移到 ProductListView 类。

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.

这篇关于对象没有属性“ object_list”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆