#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Write a view to list all the quotes
0:00 Okay, finally we get to the views.
0:03 And here there will be quite some work.
0:05 We're going to write five views.
0:08 And again let me open urls.py to guide us.
0:15 First I need to import a couple of things
0:17 we're going to use.
0:25 So let's make some stubs.
0:36 Here we will receive the primary key argument
0:39 we defined in urls.py.
0:52 So this is the basic structure of the view.
0:56 We can close this out.
0:58 Save this and here we want to go back to the ORM of what
1:02 learned at the end of day one.
1:05 And we want to query the code model.
1:09 So first I need to import the model.
1:14 And I'm using the relative import
1:17 because models is in my folder.
1:23 And I'm already going to import the QuoteForm
1:28 we made in the last video, because we will need that
1:31 in the create and update functions.
1:33 So now to get all the quotes we can use the ORM.
1:38 And just remember we can do an objects.all
1:41 and that gets us all the quotes.
1:43 Easy right, we don't have to write any SQL.
1:45 That's all abstracted away thanks to Django's ORM.
1:49 Now I need to introduce another construct
1:51 to pass that in to a template.
1:54 And Django comes with a handy render function
1:59 which I can pass into request
2:02 which we pass into the function.
2:05 So every view function gets a request by default passed in.
2:10 I'm just passing that through to render.
2:12 Then I specify a template, which we will create in a bit.
2:20 And we're going to pass in a data
2:23 that we will access in that template.
2:27 So I'm going to quotes and here pass in quotes.
2:33 And quotes is of course the list
2:36 or the query set of all the quotes we got from the ORM.
2:41 This is it this is a valid view function.
2:45 The only thing we now need to do is to create a template.