Welcome back, everyone, to this section on user authentication.
Well, we're going to do now is explore how to handle users and sessions with Django's built in tools.
Well, create a simple library website where users have profiles and books.
Let's get started with a simple skeleton of the site.
I'm going to head over to voice code.
All right.
So here I am in my Django Lectures folder, which is on my desktop, which is completely empty right
now.
And what I'm going to do is just start a project.
So let's say Django admin.
Start project and I'm going to start my library project.
Go ahead and hit enter and then you should see a pop up.
So you can now see.
Into your library.
So you notice we have our library and remember that double use case of the directory.
So a library underneath a library directory, then you have your settings image.
Okay, so now what I'm going to do is just create a simple app for the catalog.
So for example, in the catalogs of books, so we're just setting up the basic skeleton here.
So what I'm going to do is, first off, make sure you're actually inside of library so you can run
the following.
I'll say Python, manage that pie and I'm going to start an app called Catalog Enter and you should
see Beyond Library your actual catalog application that can then hold things like models for books or
views for actually expressing different things that are inside your catalog.
Now, as we continue, we want to make sure that we actually register the catalog application.
Sometimes we do this later on, but typically it's nice just to immediately after creating the application,
go ahead and register it.
Remember inside settings dot pie, if you scroll down, you have the installed apps.
So that's where we actually want to add in the catalog.
So we're going to say here.
And make sure you don't forget that comma.
I'm going to put this on a new line so it looks like everything else I'm going to say catalog the app's
dot catalog.
Config catalog.
Config.
And if you're wondering, where do you actually get this line of code?
Remember, if you go to app stop pie, you see it right here.
Catalog config obviously could be different depending on what you decided to name your application.
So we can see we have inside catalog the app's catalog config.
So if you come here, you have catalog, app store catalog config.
All right.
So that's where I'm getting that from.
So we can go ahead and save those changes inside settings that py.
Later on we'll be doing things like running migrations, etc..
And since we're probably going to have views associated just with the catalog, let's begin mapping
our URL's file.
So what I'll do here is I'm going to go to URLs, dot pie and eventually expand this to actually link
to the catalog.
Now to do that, I have to actually create my URLs that py file inside of catalog.
So inside of catalog I'm going to say new file and as we've done before, call it urls dot py.
So this is the URLs for my catalog views.
So just keep that in mind.
But now that I have this URL, let's just go ahead and quickly link it at a settings or project level.
So we already know how to do this.
We've done this a bunch of times.
So we're going to say import path, but also remember to import include and let's use it here.
We're going to say path.
Goes to catalog.
And zoom in here.
One more for you.
So you can really see what I'm typing here.
Catalog for Slash and then I'm going to include and this is where we can say catalog your URLs and don't
forget your comma here.
Always be careful of commas when you're doing things with lists.
These are kind of hard to debug sometimes, so we'll go ahead and save that change.
Added in the connection to the path for you URL patterns from my catalog application.
And one of the things I'm going to do here that we haven't really done before is actually automatically
redirect the homepage to go to catalog.
So just to give you an example, typically when we've been actually working on our projects, we'll
do something like this example, you know, dot com, and then they have to go forward, slash to the
app that we're working with.
But wouldn't it be nice if we could just go straight from example dot com and then for them to.
Example dot com forward slash app.
Typically we've been living or leaving the actual index page blank and then having to manually type
out that first application.
What I'm going to do is just have an automatic permanent redirect to go from the base domain to the
actual application where it's going to be local host goes to local host forward slash catalog.
Now we already know about some built in views and built in models and just built an import.
So jingo, the easiest way to do a redirect permanently is the following way we can say and you have
to actually import this from generic fields or say from.
Jingle dot views dot generic imports a redirect view.
And then here inside the URL patterns, you just need to put in this code.
You're going to say path.
The home path, which is just nothing or but that's the local host.
And then you're going to say redirect view.
As few.
And you're going to pass in the URL to redirect to.
Obviously, we still need to fill out those views, but just doing this ahead of time, we should now
with this line of code, be able to redirect the blank, which is essentially the domain to this blank
forward slash catalog.
Okay.
So we still need to actually set up our primary view for the catalog, inside views and URLs, but so
far we're now actually redirecting.
So make sure to save those changes.
And now to finish this off, let's create a very basic view for the catalog home page.
So we'll come back to views here.
I'm going to create just a very simple view that essentially just shows some sort of version of Hello,
just so we can confirm that this is all working.
So I'm actually going to go first to your URLs that PY and let's go ahead and set this up.
So we're going to say from Django, dot URLs, import path and then we can say from that import.
Views.
And set up our URL patterns.
This is early patterns for just the catalog app.
So here I'm going to say path.
Homepage here.
View start.
And let's just make this the index view.
Give it the name index.
And now after we save that, I can create my index view since I know its name though.
So say the index and then we're going to do is taking the request here.
And then you can just return just a simple hello.
You don't need to return an entire template right now or work on that later.
So then to show that, let's just actually show a simple HTP response.
We'll say from Django, the HTP import HTP response.
There we go.
And then say here.
Return to the pure response.
Something like.
Hello.
Okay.
So we've done a lot so far, but basically nothing we haven't really seen before, minus the little
quick fix of redirecting the view.
So we have a little skeleton.
And now we should be at a point where we can then start adding tons of URLs, views and models to hold
everything.
I do, however, want to make sure this is actually all working.
So we're going to go here and say go ahead and Python manage the API and then let's run the server.
So going to say Python.
Manage that pie run server.
Let's make sure we don't actually have any typos on anything.
So we haven't run migrations yet, but it looks like we do have something on our local.
So I'm going to go to our local on my actual computer here.
So let me bring in my browser and it's going to redirect to here.
And if you do that, you should get the automatic redirect to something that just says hello.
So here I can see hello.
And if you look at this, you should have seen the redirect happen.
So to get HTTP 1.1, it gives us a three or two redirect to catalog.
And then here we have this.
Okay, so it looks like everything's working.
And now what we can do is in the next lecture, begin focusing on our models for everything in the catalog.
I'll see you there.