We could quite easily write the book list view as a regular function (just like our previous index view), which would query the database for all books, and then call ) — a class that inherits from an existing view.
Because the generic view already implements most of the functionality we need, and follows Django best-practice, we will be able to create a more robust list view with less code, less repetition, and ultimately less maintenance.
Open /catalog/and copy in the line shown in bold below. The view function has a different format than before — that's because this view will actually be implemented as a class.
We will be inheriting from an existing generic view function that already does most of what we want this view function to do, rather than writing our own from scratch.
In this tutorial we're going to complete the first version of the Local Library website by adding list and detail pages for books and authors (or to be more precise, we'll show you how to implement the book pages, and get you to create the author pages yourself!
) The process is similar to the creating the index page, which we showed in the previous tutorial.
I would have thought the challenge would be a little more forgiving.
This tutorial extends our Local Library website, adding list and detail pages for books and authors.
This does all the work of creating an instance of the class, and making sure that the right handler methods are called for incoming HTTP requests.
We'll show both list and detail views after the next section.
The book detail page will display information about a specific book, accessed using the URL ) including the status, expected return date, imprint, and id.
The code inside the loop creates a list item for each book that shows both the title (as a link to the yet-to-be-created detail view) and the author.
Note: We have to be a little careful of "side effects" when calling functions in templates.