我想创建一个包含各种可选过滤器的列表视图。我的网址应该是这样的:
shop/category_6/?manufacturer_id=3
当我将此网址放入浏览器表单时,它运行良好。但是当我尝试将此选项添加到模板时,它会给我NoReverseMatch。我的模板:
{% for manufacturer in manufacturer_list %}
<p><a href = "{% url 'shop:item_list' category_id=category.id manufacturer_id=manufacturer.0 %}">{{manufacturer.1}}</a></p>
{% endfor %}
这是我的观点:
class ItemListView(ListView):
model = Item
context_object_name = 'item_list'
template_name = 'shop/item_list.html'
def get_queryset(self, **kwargs):
full_item_list=Item.objects.all()
queryset=full_item_list.filter(category__id=int(self.kwargs['category_id']))
manufacturer_id = self.request.GET.get('manufacturer_id')
if manufacturer_id:
queryset=queryset.filter(manufacturer__id=int(manufacturer_id))
return queryset
def get_context_data(self, **kwargs):
context = super(ItemListView, self).get_context_data(**kwargs)
context['category']=get_object_or_404(Category, pk=self.kwargs['category_id'])
context['manufacturer_list'] = self.get_queryset(**kwargs).values_list('manufacturer__id', 'manufacturer__name').distinct().order_by('manufacturer__name')
return context
我的错误是什么?
答案 0 :(得分:2)
您没有配置GET参数...可能是shop / category_6 /?
manufacturer_id = 3
<a href = "{% url 'shop:item_list' category_id=category.id %}?manufacturer_id={{ manufacturer.0 }}">{{manufacturer.1 }}</a>