#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Write a view to list all the quotes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay, finally we get to the views. And here there will be quite some work. We're going to write five views. And again let me open URLs.py to guide us.
0:16 First I need to import a couple of things we're going to use. So let's make some stubs.
0:37 Here we will receive the primary key argument we defined in URLs.py. So this is the basic structure of the view.
0:57 We can close this out. Save this and here we want to go back to the ORM of what learned at the end of day one. And we want to query the code model.
1:10 So first I need to import the model. And I'm using the relative import because models is in my folder. And I'm already going to import the QuoteForm
1:29 we made in the last video, because we will need that in the create and update functions. So now to get all the quotes we can use the ORM.
1:39 And just remember we can do an objects.all and that gets us all the quotes. Easy right, we don't have to write any SQL.
1:46 That's all abstracted away thanks to Django's ORM. Now I need to introduce another construct to pass that in to a template.
1:55 And Django comes with a handy render function which I can pass into request which we pass into the function.
2:06 So every view function gets a request by default passed in. I'm just passing that through to render.
2:13 Then I specify a template, which we will create in a bit. And we're going to pass in a data that we will access in that template.
2:28 So I'm going to quotes and here pass in quotes. And quotes is of course the list or the query set of all the quotes we got from the ORM.
2:42 This is it this is a valid view function. The only thing we now need to do is to create a template.


Talk Python's Mastodon Michael Kennedy's Mastodon