Challenge 1: Render a List With Filters — Possible Solution ====================================================================
No books in the catalog yet.
{% endif %} from django.shortcuts import render def book_list(request): books = [ { "title": "Dune", "description": "A boy becomes the prophesied leader of a desert planet's " "native people after his family is betrayed and destroyed " "by a rival house, setting in motion a galaxy-spanning war.", }, { "title": "1984", "description": "A man working for a totalitarian government's propaganda " "ministry begins to question the Party's control over " "truth, memory, and thought itself.", }, ] return render(request, "catalog/book_list.html", {"books": books}) WHY THIS WORKS -------------- - {% if books %} checks truthiness the same way Python does — an empty list is falsy, so the {% else %} branch renders the fallback message automatically whenever the context variable is missing or empty, without needing a separate {% if books|length > 0 %} check. - {{ book.title|upper }} applies the built-in `upper` filter, converting the string to uppercase entirely within the template — no changes needed in the view or the underlying data. - {{ book.description|truncatewords:15 }} cuts the description down to its first 15 words and appends an ellipsis automatically — useful for list views where a full description would be too long, while the full text remains available on a detail page. - forloop.counter (from the chapter) wasn't strictly needed here since the challenge didn't ask for numbering, but could be added the same way shown in the chapter if a numbered list were wanted instead of a plain bulleted one.