我正在使用 Django-table2 作为 SingleTableMixin,并且收到此错误,但我无法解决该错误。错误说我需要在查询中提供数据才能使其工作。以下是代码并请求指导。
class ProductListView(SingleTableMixin, generic.TemplateView):
template_name = 'dashboard/catalogue/product_list.html'
form_class = ProductSearchForm
productclass_form_class = ProductClassSelectForm
table_class = ProductTable
context_table_name = 'products'
def get_context_data(self,**kwargs):
ctx = super(ProductListView, self).get_context_data(**kwargs)
ctx['form'] = self.form
ctx['productclass_form'] = self.productclass_form_class()
return ctx
def get_description(self, form):
if form.is_valid() and any(form.cleaned_data.values()):
return _('Product search results')
return _('Products')
def get_table(self, **kwargs):
if 'recently_edited' in self.request.GET:
kwargs.update(dict(orderable=False))
table = super(ProductListView, self).get_table(**kwargs)
table.caption = self.get_description(self.form)
return table
def get_table_pagination(self):
return dict(per_page=20)
def filter_queryset(self, queryset):
return filter_products(queryset, self.request.user)
def get_queryset(self):
queryset = Product.browsable.base_queryset()
queryset = self.filter_queryset(queryset)
queryset = self.apply_search(queryset)
return queryset
回溯:
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in get
158. context = self.get_context_data(**kwargs)
File "C:\Users\AliKhan\supermarket\market\dashboard\catalogue\views.py" in get_context_data
77. ctx = super(ProductListView, self).get_context_data(**kwargs)
File "C:\Python27\lib\site-packages\django_tables2\views.py" in get_context_data
107. table = self.get_table(**self.get_table_kwargs())
File "C:\Users\AliKhan\supermarket\market\dashboard\catalogue\views.py" in get_table
91. table = super(ProductListView, self).get_table(**kwargs)
File "C:\Python27\lib\site-packages\django_tables2\views.py" in get_table
82. table = table_class(self.get_table_data(), **kwargs)
File "C:\Python27\lib\site-packages\django_tables2\tables.py" in __init__
348. self.data = self.TableDataClass(data=data, table=self)
File "C:\Python27\lib\site-packages\django_tables2\tables.py" in __init__
45. ' list(data) -- {} has neither'.format(type(data).__name__)
Exception Type: ValueError at /dashboard/catalogue/
Exception Value: data must be QuerySet-like (have count() and order_by()) or support list(data) -- NoneType has neither
最佳答案
您的 Django settings.py 无法收集 django-table2 及其依赖项。尝试从 shell 导入它。代码没有问题。你在尝试虚拟环境吗?如果是,那么哪个操作系统?如果是 Windows,那么您必须先全局安装 django-oscar,然后才能在 virtualenv 中尝试。
关于python - data 必须类似于 QuerySet(具有 count() 和 order_by())或支持 list(data) —— NoneType 两者都不具备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41529097/