#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Make a view/page to view an individual quote
0:00 Alright, let's write some more views.
0:03 Again, I go in to my quote app and open the views
0:07 and let's first write our quote_detail.
0:11 But first, let's let do it the way
0:13 and then I'll show you a nice shortcut.
0:17 We're going to try to get
0:24 a primary key that's parsed into the view
0:28 and if that ID is not found, then typically it raises
0:32 a quote does not exist exception.
0:38 And in that case, you would raise a HTTP 404.
0:43 Now this is so common, that we look up an item
0:47 by primary key, that Django has a nice shortcut
0:52 and it lives in django.shortcuts
0:53 as get_object_or_404.
0:56 And it reduces these four lines to just this.
1:04 Pass in a model
1:07 and the key word PK, or primary key
1:10 which again, is parsed in, and that's it.
1:13 If the quote is found, it's stored in the quote variable.
1:18 If not, it raises a 404 for us
1:20 we don't have to worry about it
1:22 that's what the helper function will do.
1:26 So that's pretty cool.
1:28 Now we're going to make another template
1:33 and use render again with request
1:36 the second template, which we'll write in a second.
1:44 And of course we get a t quote.
1:50 And that's it, that's all that's needed for detail.
1:55 And to make a new template, quotes
2:03 And to avoid too much typing of html
2:05 I'm going to paste that in.
2:13 And we see this nice inheritance going on
2:15 with again, extending from base.html.
2:17 So you get all the html rounded.
2:20 We use Humanize again for the date
2:23 and here in content, we put the stuff that is relevant
2:26 to this template.
2:28 So we have a bit of a different view here
2:30 because it's a single quote, so we put the quote
2:32 in the block boat, we put inside special html.
2:38 Again, we have an edit button.
2:41 Now we make a link back, to go back to the main page
2:45 where all the quotes are listed.
2:46 So here we link to the quote list view
2:51 and that's it really.
2:53 We can save this and try it out.
3:01 Awesome, look at that.
3:06 This one does not have a background; the other one has.
3:09 And how is this background loading?
3:12 Well, if you remember in the base template
3:18 we added this to the body.
3:21 If there's a quote parsed to the template
3:23 and I know that the quote variable was parsed
3:25 to the quote template, but has an inheritance
3:28 from base html, this template is effectively is culled
3:31 as well, so the same goes here.
3:34 The quote is actually parsed to this template as well.
3:37 So here in this case, the quote was found
3:39 it has a cover, so the background is set on the body element
3:45 and it makes that the background cover scales
3:48 to the whole website.
3:50 So that's the view of the individual quote.
3:53 Next, let's write the view to add a new quote.
3:56 You may fill or use the form we defined earlier.