我正在使用django mptt
以下是我的观点:
def category(request, path, instance, extra):
if instance:
instance.save()
queryset = Product.objects.filter(category__in=instance.get_descendants(include_self=True))
color = request.GET.get('q')
if color:
queryset = Product.objects.filter(optiongroup__option = color )
return render(
request,
'products/prodgrid.html',
{
'instance': instance,
'children': instance.get_children() if instance else Category.objects.root_nodes(),
'queryset': 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:\Users\lenovo\Desktop\plump\Plumpin\src\mptt_urls\__init__.py" in __call__
35. return self.view(*args, **kwargs)
File "C:\Users\lenovo\Desktop\plump\Plumpin\src\products\views.py" in category
35. queryset = Product.objects.filter(category__in=instance.get_descendants(include_self=True))
Exception Type: AttributeError at /category/women/women-clothing/dresses/
Exception Value: 'NoneType' object has no attribute 'get_descendants'
这是模板
{% for color in instance.get_colors %}
<li><label for="{{color.value}}"><input type="checkbox" id="{{color.value}}" group = "color" value="{{ color.value }}"/><a href="{{ instance.get_absolute_url }}/?color={{ color.value }}">{{ color.value }}</a></label></li>
{% endfor %}
我在哪里错了?
如何改进代码?
答案 0 :(得分:0)
您的代码在此行中失败:
queryset = Product.objects.filter(category__in=instance.get_descendants(include_self=True))
因为实例是None
。您需要弄清楚为什么category
方法已使用instance=None
进行调用。你希望在哪里设置实例?