Welcome back, everyone, and the previous lecture, we used a create view to create a new instance
of our model in this particular case, a teacher.
Now I did tell you you could go through the admin panel to confirm that actually worked, but we could
also do is create a list for you, which is going to list all the instances for a particular model.
Let's check it out.
OK, so here I am back underneath views.
What I'm going to do is create a new view that can list all the instances and any particular model.
So we scroll back up here and again from jangled up views that generic I'm going to import a list view
and then we'll scroll down here and it's going to look quite similar to the create view.
As I mentioned earlier, these are going to start to look a little repetitive, but that's kind of the
point.
It should be easy, repetitive and simple to remember how to use these views.
So we're going say class and we'll say teacher list view and we inherits from list view.
And again, all we need to do is connect this to the model.
That we're looking for.
So we'll say connect it to the teacher model, and actually it could just work with that.
So I'm going to save this for right now.
Essentially, all this says is list every instance of a teacher now by default.
This also has a template name that it looks for, and that template name is going to be model underscore
lists dot each HTML in the same way we had great view.
Look for teacher underscore form, but it's HTML.
This is not looking for model underscore lists that each HTML.
In fact, let's create that teacher list now.
So I'll go to a classroom.
Create a new file and call it teacher underscore list.
Thought each HTML.
And this is going to be a list of all the teachers.
So say each one list of teachers, and I'll let you know it's a list of you.
And then what I'm going to do here is create an ordered list.
And this is where I can begin to use some jingo.
So I'll say four and I'll say for teacher.
In and then object underscore list, I'll talk more about how I know this called object list later on.
And then what I want to do is pretty much for every teacher here.
I'm going to create a list item and we can just say this is a teacher, and let's just use, let's say,
their first name.
And then we'll insert teacher.
Last name.
OK.
And that's pretty much what we're going to do here now.
Next, I need to actually connect this view to a URL, so we'll go ahead.
And after we save that change under teacher list of you come to your URLs and imports on, I start breaking
this up into multiple lines of parentheses, just so it's easier to see all the views I'm importing.
So I'm going to import list.
Actually, it's teacher list, view, teacher list, view close parentheses there to have this on multiple
lines, and let's create that new URL path will say this goes under list underscored teacher or teacher
lists kind of whatever you really want to call it here.
It's just the URL path.
And then we say teacher list of you.
And remember for these because they're class based views and path expects a function.
We say as view, and then I can give it a name like list teacher.
OK.
Save those changes, and the last thing I want to do is actually connect this to a link inside home
that HTML.
So I will copy what we've been doing before.
And then create a new list item here, but this will be not great teacher, but list teacher, and then
we'll say this is the List Teacher page link.
So let's save those changes and come back to my original welcome page.
Make sure we didn't commit any typos or errors.
So now my welcome home page, I have that new link list teacher page link.
I click on that and I see a list, the teacher list view.
Jose Padilla the first name and the last name.
Let's go back and go to create New Teacher Page Link and check it out just to confirm it's working.
So I'll say Adam Smith and subject, let's say economics.
OK.
Hit submit.
We're now at the Thank You page.
And now let's go back to our home page and then what we can do here is check out the List Teacher Page
Link and now I see Adam Smith perfect.
Now, let's discuss a few more options for the list of you, so by default, you can see it's extremely
simple to set up a list of you.
You just connect the model and boom.
By default, it's going to do a query request for all the objects inside that model and then just send
them back.
And what it does is it sends them back as this object underscore list now object underscore list.
This can be a little cryptic if maybe you have a really confusing set of views.
Not really confusing, but let's say you have a lot of use and you just have a front end developer who's
just looking at this and they start scratching their head.
What is object list?
What you could do is actually change this variable name.
The way you do that is you come back to views and then here on their teacher list view what you can
say is context.
Object name.
And then you pass in a string of what you actually want to have as the object name, so then you could
say something like teacher list and then instead of the object list, you could call this teacher list.
So let's confirm that worked.
You should be able to just come back here and refresh this and you see, have the teachers working again.
And if you ever want, you can come back here, create a new teacher.
Let's call this third teacher.
Teaches math, submit.
Come back to the classroom page, and we may want to put a link to the home page, and we'll be doing
that a lot and then we can go to last page here.
Jose Arrowsmith, third teacher So it looks like that's all working again that allows you to edit what
this particular variable name is.
I would recommend that you typically overwrite that object list can be a little confusing, especially
as you get more and more views and more and more HTML files.
So it's much easier to understand what teacher list is than what object list is.
OK, so the other thing on a point out is you actually have a lot of flexibility over what actual query
is created to grab all the instances in this model.
So what do I mean by that?
Well, what you could do is call query set, and let's make sure we spell that right.
Query set is equal to.
And here you can override the default query.
By default, your query looks something like this.
It's just saying teacher objects.
All this is essentially the default query set that a list view does.
But perhaps you may want to actually sort or order the teachers.
So it's really up to you.
What other factors you may want inside this query set?
In fact, later on then you can have multiple lists for different factors.
But what you do is you override the actual query set that ends up being sent back as this context object.
So, for example, I can say teacher the objects and then say something like, well, actually order
by an order by, let's say, their first name.
And this is where you can use all the skills you learned back in the model section on things about querying
or filtering, etc. You can use that filter that order by whatever you want really to override this
query set that way.
Maybe you don't want to actually list every single teacher, but only maybe certain teachers or something
like that.
So you just override the query set attribute, go ahead and save that change.
And then I'm going to come back here to list the teachers and to refresh this, and you can see now
it's in alphabetical order.
A J.
And T..
OK.
So those are the three main attributes that you should know about for a list view.
Again, the one that's required is to connect it to a model, and we saw technically a one liner.
That's all you need to do.
But then if you want to edit the context, object name that's passed back to the model underscore list
h html.
You can overwrite that this attribute, and you can even overwrite the actual query set on what does
actually mean by a list of everything.
OK.
Pretty cool.
Now, so far we have the ability to create a new teacher and list all the teachers.
But imagine I wanted to be able to get the specific details of a teacher.
Notice here.
I don't actually see what each teacher ends up teaching as a subject, so maybe I want to actually get
what's known as a detail view.
We'll learn about that in the next lecture.