django - 'ProductList' 对象没有属性 'object_list'

标签 django python-2.7

在我的 ProductList上课,当我尝试调用 get_context_data在另一种方法中,我得到一个错误 'ProductList' object has no attribute 'object_list'

def get_context_data(self, **kwargs):
        c = super(ProductList, self).get_context_data(**kwargs)
        c['category'] = self.category
        c['category_menu'] = self.get_category_menu()
        c['filters'] = self.filters
        c['expanded_filters'] = self.get_expanded_filters()
        c['active_filters'] = self.get_active_filters()
        c['category_list'] = self.category.get_children().filter(in_lists=True)
        c['colors_list'] = self.get_colors_list(c['object_list'])
        return c

def get_queryset(self):

    data = self.get_context_data()

是什么导致了这个错误?

我怎样才能得到object_list在我的第二种方法中?

最佳答案

您可能会在关注 line 时遇到错误在 get_context_data()父类(super class):

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

get BaseListView的方法设置object_list在 View 上调用 get_queryset方法:
self.object_list = self.get_queryset()

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

关于django - 'ProductList' 对象没有属性 'object_list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37675704/

相关文章:

python - Pandas groupby 找出正确和错误的百分比

python - 为什么 python 脚本在主进程退出后挂起

php - 在 Python WSGI 环境中运行 PHP 脚本

python - Django OneToOne 字段 self

javascript - 在 Django 中重用 javascript 函数来 Bootstrap 表

Python __super黑魔法失败了

Python:转换为字符串时如何近似 float ?

python - django 中硬编码 urlpatterns 的替代方案

python - 无法使用 django 1.4 运行 mongodb

Python 2.7 - Pygame - 相交两个移动 Sprite