[Answer]-I'm having trouble creating a drop-down sort menu for an e-commerce website

1👍

Use a class based generic view (ListView). Have a good read of the docs. Specifically the sections on viewing a subset of objects and dynamic filtering.

Personally I think you should drop the form all together and add your own <a> tags to a template. So maybe read up on the URL dispatcher too. I think you need the reverse() function (possibly in the template).

As a basic outline –

from django.views.generic import ListView
from books.models import Item

class CategoryListView(ListView):

    template_name = "item_list.html"

    def get_queryset(self):
        category = name__iexact=self.args[0])
        return Item.objects.filter(publisher=publisher)

You need an entry in URLconf to get the self.args[0] and you’ll need to write a template for "item_list.html"

Leave a comment