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