Personal Catalogue: Django & PostgreSQL — Chapter 7, Exercise 1 ==================================================== TASK Build the icontains/Q-object version of search from this chapter, then confirm /?tag=python&q=fastapi genuinely returns only items matching both conditions at once. SOLUTION 1. catalogue/views.py — update ItemListView's get_queryset() and get_context_data(): from django.db.models import Q class ItemListView(ListView): model = Item template_name = 'catalogue/item_list.html' context_object_name = 'items' paginate_by = 25 def get_queryset(self): queryset = Item.objects.select_related('item_type').prefetch_related('tags') tag_name = self.request.GET.get('tag') if tag_name: queryset = queryset.filter(tags__name=tag_name) query = self.request.GET.get('q') if query: queryset = queryset.filter(Q(title__icontains=query) | Q(creator__icontains=query)) return queryset def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['active_tag'] = self.request.GET.get('tag', '') context['query'] = self.request.GET.get('q', '') return context 2. catalogue/templates/catalogue/item_list.html — add the search form above the item list, with value="{{ query }}" so the box shows what was actually searched for after submission. TEST SETUP AND CONFIRMATION Add (or confirm you already have) at least one book item titled something containing "FastAPI" that is also tagged "python", and at least one other item that matches only the tag or only the search term but not both (for example, a Python-tagged book with a title that doesn't contain "fastapi", and a non-Python-tagged item whose title does contain "fastapi"). Visit /?tag=python&q=fastapi directly. The results should include only the item(s) satisfying both conditions - tagged "python" AND with "fastapi" somewhere in the title or creator - excluding both of the partial-match items described above. WHY THIS WORKS AS AN ANSWER ---------------------------- It implements both filters exactly as chained in the chapter, and specifically sets up test data designed to distinguish "matches both" from "matches only one," which is the only way to actually confirm the AND-combination behavior rather than just confirming each filter works in isolation.