hopheylalaley
hopheylalaley

Reputation: 69

Setting GET parameters in url in Django

I want to create a listview with various optional filters. My url should be look like this:

shop/category_6/?manufacturer_id=3

And it works well when I put this url just in to the browser form. But when i try to add this option to a template it gives me NoReverseMatch. My template:

{% 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 %}

It is my view:

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

What is my mistake?

Upvotes: 0

Views: 90

Answers (1)

Raja Simon
Raja Simon

Reputation: 10305

You didn't configure GET params... May be shop/category_6/?manufacturer_id=3

<a href = "{% url 'shop:item_list' category_id=category.id %}?manufacturer_id={{ manufacturer.0 }}">{{manufacturer.1 }}</a>

Upvotes: 2

Related Questions