Welcome back, everyone, to this lecture.
We're going to begin setting up some views for our actual pages.
Things like a homepage that can display all the books available for checkout or the status of all the
books in the library.
Let's go ahead and head back to our code editor.
Okay.
So here we are back at our code editor.
Let's go back to our views that PI.
Remember, we actually already had a very simple index page that just responded, hello.
Let's now use models to start displaying information from the models.
So say from that model's imports.
And I'm going to import book.
Let's say author book instance.
And we can also say the genre in language whether or not will actually display this.
We can decide later on.
So here we're going to have just a simple view function and let's generate some counts of some of the
main objects in our databases.
For example, the number of books, maybe that's something we want to display.
So I can say book the objects the all.
And then simply request a count of them.
And then I can also say, what are the number of instances of books?
So remember, this is books as in like the titles, but how many actual copies do we have available?
So a number of instances would be very similar.
It would just be book instance.
Objects all.
And currently the way we've set this up, there's just one copy of that mystery novel, and then maybe
we can even have a number available.
So, for example, number of instances.
Available would just be the book instance.
Objects and then we filter.
So what do I filter?
Well, I could filter based off its status.
So I say status underscore, underscore exact number A was our code for being available.
And then from there, I could create that count.
So so far reporting back the number of books, essentially them are titles hold a library, number of
instances.
So total physical copies, number of instances available for checkout.
And what I can also do is just say number of authors, etc., but eventually we have to pass all this
information back.
So I'm going to say return, render the request and here I'm going to reference a template that doesn't
exist yet.
Index each HTML.
I'm going to pass this back as context.
So let's actually create a context dictionary for this.
Also, context is equal to context.
But then I'm going to actually paste this in.
So here we'll say context is equal to the dictionary.
And we're just going to have the key value pairs match up to their names.
So this will be equal to number of books, comma.
Number of instances.
His number of instances, comma.
And the number of instances available.
Number instances available.
Common instances of ill.
There we go.
Okay, so here's our context.
We're passing it back to index that each HTML, which means I actually need to post that in somewhere.
So let's create a new file here.
So we'll say new folder, actually templates.
And then inside of templates, we're going to see a new folder called catalog.
And then inside of this is where we're going to create that first index that each HTML file.
So now I can start typing out each HTML and then in fact, we can make this a full dock so I can say
talk.
And then inside the body, have a heading saying.
Homepage.
And then I could start actually inserting things.
So, for example, I could say, go ahead and inserts the number of books we have.
So a paragraph tag.
Um.
Total books.
And then this is where you would actually start inserting things.
So I just do that with a call here.
Double set of brackets.
And for example, I can see a number of books.
Okay, let's just do one more example, since I'm sure you guys kind of get it by now.
So a number available.
Here we go.
And let's have the say something like number of instances available.
Now, remember, we're passing that back through the context.
So I'm going to go ahead and save this.
And then for now, last thing I want to do to make sure it's actually all gets linked up and displayed
because it's under templates and catalogue.
What I could do for simplicity is just say, Here we go, here.
Catalog forward slash index each HTML.
Okay.
And then if you actually come here to your homepage, you should now see something that looks like this.
Let me zoom in for you.
You can see it says total books are one number available is zero.
And that matches up to what I created last time I did create a book, but then I said it was checked
out and it was due tomorrow.
So clearly we have a connection on the homepage and you could begin to link any information.
Made a small little typo there, but no big deal.
So we want to begin to explore is the ability to do things like edit maybe or add, update, delete,
etc. based on certain views.
But what I want to eventually add in is a capability to only be able to visit some of these views based
off user authentication.
So we're going to set up a couple of more views.
We're not going to do absolutely all of them, but you can eventually add in more and more views and
you can check out the full code if you want.
Or just a code that we've worked on here together in this video but eventually want to do is restrict
views or restrict capabilities based off the user themselves.
Like are you actually an official user and do you have permission to do something on our website?
Like a librarian.
So for right now, let's create a simple class based view that is going to allow anybody right now on
the page to create a book.
Then we'll switch over now that we've done all the work to set up a very skeleton website of a library
to actually show you, Hey, this is how you can mix in the permissions of requirements.
So let's go ahead and create one more view.
I'll actually do this one as a class based view so we can see that as an example here.
So we're going to say from.
Django doc views the generic.
And I'm going to import.
And let's import a very simple, creative view.
And if we wanted to, I could continue doing this with a model view, but we'll keep things simple for
now.
So I have this very simple view, and then what I'm going to do is create a view that essentially connects
to a model.
So here we're going to say.
Class book creates.
So the ability to create a new book keep minds.
Not the creation of the book instance, just the creation of a book.
And then we're going to inherit from create view.
And you could do this for creation of authors, creation of languages, etc..
Keep things simple for now so we don't just do a bunch of duplicate work.
So say book, create, create view, connect it to a model.
So going to connect it to the book model.
Remember, I already imported that up here.
And then what I'm going to do is say the fields that they can create.
So we're just going to say all.
And then if you wanted to continue, you could give this a success.
Earl Maybe we'll add that in later, but let's actually connect this to the default, remember, for
create view.
Sometimes it's easy to forget, but it's going to be your model underscore form that each HTML, which
means it will be looking for the book underscore form the HTML inside templates, which means are going
to come in here to catalog, going to create a new file and we are going to call this book underscore
form, start each HTML and then here we'll just go ahead and say create new book.
And then the form.
Will be.
We don't need an action here, but we do need a method or same method is post.
Is equal to post.
And then we have the CSR a token.
So or a token.
There we go.
Oops.
Looks like XLE auto completed that twice.
There you go.
There's a CSR token and then we'll also just pass in the form.
So it's a form and we'll make this as paragraph.
Finally, the input button itself will say input and it's going to be just input type is submit.
The value can be something like.
Create book or just create.
Okay.
So make sure you save those views as well.
We create a very simple create view.
Obviously, you can use the power of generic views to create list views, to list all the books that
are available, unavailable, etc..
We already know how to do that with filters.
You can do delete views for deleting books, but right now keeping it as simple as possible in this
skeleton for the purposes of understanding users later on.
So we have this create view but have actually linked it to any specific URL.
So we'll come back to your URLs here at the actually, we need to go to the catalog level.
So let me click here.
Okay.
So we have the home page, but we don't have an actual view for the creation or the excuse me, the
URL for the creation.
So to fix that, it's pretty simple.
We just are going to say from the import views or what I can also say is actually we'll just leave it
as views.
I don't mind calling it again.
I'm going to set a path called, let's say, create book should be okay.
And then I'm going to say this is VUSE Dot and then we called this book Create.
Here we go.
Put creates as view because remember these actually expect functions.
And then we'll say the name of this is Create Book.
Okay.
So that should now link it to a URL pattern.
So if you go to homepage forward slash, create book or actually catalog for its last great book, then
it should take you to this view for the creation view.
And let's make sure that's all working.
We may have committed a typo along the way, but no problems.
We should be able to fix it.
So here I am at my home page for the catalog.
Let's go to create book.
Let me make sure that was the actual URL that we just described.
Create book.
So I'm going to go there, create underscore book, and I bring that in here.
So we do see the ability to create a book.
So I'm going to say the title is brand new test book.
The author notice it's a dropdown so we only have Jose Pretty.
The choose from summary is test book obviously super ugly form, but you get the idea ISBN number.
This is something you would like look up on the internet and the mystery.
I think the book's like 13 some slowly, something like that.
We'll also have this one B mystery.
We could create genres if we wanted to.
And then English here from the dropdown some hit create make sure we don't get error.
It looks like we do get a reverse match error.
So book detail is not a valid.
Oh, I see.
So basically by default, the success page is to go for the book detail.
But currently, we don't technically have a view that shows the book detail.
So while I believe that book should have been created and we can check the admin to actually check if
that's true, currently there's no reverse functionality to go to the book detail page.
So what I'm going to do here is I'm actually going to go to admin and confirm that book was created
just to check that our form is working.
So here we go to books and I can see my brand new test book was created.
It's just that there was no success URL to go to and the default want us to go to detail view.
So we have two options here.
One is to manually override the success URL.
We've actually done that before.
It's pretty straightforward.
You just say you come here.
To views that pie and say success IRL and then do a reverse lazy lookup for whatever view you want to
go to.
Now by default, it is the detail view for that particular book.
So the other option we have here is to create a detailed view.
So I could come up here and from create view also imports detail view.
So it's not so bad to do that.
We'll go ahead and create a new class called Book Detail.
And we're going to call this detail view imported from.
And then we're just going to connect this to model is equal to book there which means I need a new template.
So we'll say new file and this will we book underscore detail by each HTML.
Simply going to say detail for book.
And then this passes in that actual book model.
So we'll save that.
Go to views.
Save that connection.
So make sure you save that connection there.
And so now the redirect after creating a book should take you to that book detail page, which we have
here.
Very simple, but it should be working.
So I'm going to go back to my catalog.
Let's go back to my home page here.
So we're back to the home page.
It looks like I did actually create that book, but we don't have that detail page yet.
So we're going to go to catalog and this is Book Create or might have been create ebook loops.
Let me create underscore book.
There we go, create a new book.
So we're going to call this second test.
Same authors before.
We're going to call this my second test book.
I have to be number 1 to 3.
It's also a mystery, although I believe some of these we said we could leave.
No, it's in English going to create this.
And it looks like we still have an error here.
So let's dive deeper.
Oh, it's because we forgot to actually make the URL matching.
So almost none.
And to come back here to your URLs.
And now let's fit that in.
So say path.
And then here is where we're actually going to have the detail for the path.
So in that case, we simply insert the detail view.
So we're going to say this is going to be book.
And this can be integer.
Pick forward slash and then I could then say views dot and then that would be my book detail you will
see book.
Detail.
As you.
And I could give it the name detail book.
And save that right there.
All right, so let's try this one more time here.
Make sure we're not committing an error.
So let's go back to Create Book, which is going to be the third book.
So a third test book.
And I'm going to type.
I remembered you all this time.
ISBN number.
Remember, these have to be unique.
I believe so.
You make an error if you keep repeating ISBN numbers.
Let's create.
And it looks like we actually used the wrong name.
Book detail.
Not detail.
Book.
So say book detail.
Save that and.
One more time here.
So this will now be my fourth test book.
So a fourth test book.
Here is time and selected the correct name.
Let's give this a unique spin it creates.
And there we go.
Okay.
So let me go through the steps that I did to remember how to actually get this detailed book to work.
So coming back here, we come to our views and we create this very simple view which is based off the
generic create view.
So that worked out.
But remember, in order to use that view, you have to have the form.
So we have the form and we connected that through URLs as shown here create book.
Then after creating that book, the default is to go to that book's detail page.
Now, that's where I forgot to actually create the view.
So I created the book detail page.
Then I got the error that said, I can't find this book detail view.
Well, that's because I hadn't actually connected it through your URL, so I connected through your
URL.
But then the default name I was looking for was book underscore detail, not detail or score book.
And it actually told us that in the error message.
But now I should have enough of a skeleton to focus on users.
So what do I actually have so far?
I have a home page that has the ability to look up things like books, the number available, etc. And
then I also have the ability to create new books.
So if I go to base of my URLs for slash catalog, for slash create book.
So if I go here.
Then I have the ability to create a new book and go to its detail page.
So that is the entire skeleton we need to begin to explore.
User Sessions And the main idea here is based off these views.
We already know how to create all kinds of views, such as lists, views, detailed views, all these
different class based views.
But what if I only want someone I know like a special librarian, to be able to create new books?
I don't want everyone visiting the library site to have access to this form.
So essentially when we're thinking about users, we only want people to have access to certain pages.
And that's what I'm going to focus on next now that we have the skeleton code.
So I'll see you in the next lecture.