Django: Getting Started Transcripts
Chapter: Django ORM and Databases
Lecture: Returning database results in a view

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's take all this database stuff and add it to a view. Offscreen, I've created catalog views.py.
0:10 You may remember creating the sample view a while back. It had a bunch of dictionaries representing books. This view is almost the same.
0:18 The difference is now instead of there being a bunch of hard coded dictionaries, I query all the books in the database. on line 9.
0:25 Query I'm using here is similar to the all query, but instead of just returning all the books,
0:32 however, the database wants, I'm specifying their order using the order by method. This query says order by author's last name.
0:42 Then by the book's title's, the double underscore here allows you to specify using the
0:49 authors attribute of last name following the relationship between book and author.
0:55 So this query says get all of the books ordering by the author's last name and then by title. Like the sample view.
1:04 All this is sent to the render shortcut in this case using a new template called book_listing.html. Let me open that up.
1:14 Also off screen, I created templates book_listing.html. I've been busy offscreen. This is an echo of sample.html,
1:23 this time I'm extending the base then, on line seven I have a for loop tag. The loop is iterating through all the books in the
1:31 data context, spitting out the title in bold and some author info Of course I need a catalog URLs file
1:41 Where I define the book listing path and then I have to include this file in the master alexandria URLs file.
1:51 All things you've seen before, line 18 includes all of the URLs in catalog.URLs under the catalog path.
1:59 Let me fire up the server and now I'll visit the URL. Ain't that grand? There are all our books.


Talk Python's Mastodon Michael Kennedy's Mastodon