Django: Getting Started Transcripts
Chapter: Posting Data
Lecture: Add a view for a book

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before requesting a review from a user. You're probably going to want somewhere to show the reviews.
0:06 Up until now our books don't have pages themselves. Well, web pages, that is. So let's add that. Pretty simple view here, looks up the book by ID,
0:25 then renders the appropriate template. Don't forget to add the import for the helper method. And there's the corresponding URL.
0:44 Remember that the angle brackets here indicate that this view will take an argument. In this case, that'll be the book's ID which is an integer,
0:52 All parts of the URL are a string, so indicating that this is an integer Django will convert it for you before calling the view.
1:00 And I've added a corresponding template for the view. The first section is the author's info. The second section has a link for reviewing the book.
1:15 It uses the URL template tag to look up the review book view. It hasn't been written yet. So to get this to work, I'm going to comment it out.
1:32 And then at the bottom of the review section, all of the reviews associated with the book are looped through and displayed.
1:38 You'll recall that models with foreign keys can use the model name underscore set query manager, to access the related models.
1:46 Here that would be the review underscore set to get the books reviews and the all method on that query manager to get well, all of them.
1:56 The four loop uses the bootstrap component called a card to display the reviewer's name, rating, and review text and it also has the optional empty tag
2:06 in case the book has no reviews. With all that in place let's go take a look at it. I've gone directly to book number one and this is the review,
2:20 the author and the review underneath. Let me go to number two. And there's that handy empty clause in practice.
2:31 Using the admin to write reviews doesn't seem like a very good idea, especially as most of our users won't be allowed to use the admin area.
2:40 With everything now set up, you're ready to write the code that accepts a user's review.


Talk Python's Mastodon Michael Kennedy's Mastodon