Welcome back, everyone.
In this lecture, we're going to go through some user authentication forms.
Well, if you want, you could use the built in user object to create users manually using your own
form.
Luckily for us, Django provides quite a few built in forms and views based off the user class that's
built in the Django.
Let's explore some of these in the documentation and in our code.
Okay.
So here I am inside the documentation for using the Django authentication system.
By this point in time, we should understand enough to be able to read this documentation page.
We didn't jump straight to it at the beginning of this section because there are a few concepts that
the documentation kind of assumes you already know before you actually start reading it.
So I should point out that everything we've been using has fallen under the built in user objects,
which, as we already know, are built into the authentication system.
And we've seen them inside the admin.
And we also already know that the primary attributes of a user are username, password, email, first
name and last name.
I should point out that there's full API documentation.
So if you want, you could continue to customize user objects by actually inheriting the user class
and then adding more fields to it.
If you needed something like let's say a profile picture for your particular website, you could start
linking that inside the user object.
Or probably more appropriately, could we do something like a profile picture model that we link through
a foreign key to a user, lots of different ways to actually tackle that sort of problem.
Then as I mentioned, you can create users directly.
So the way you could do this is you could say from Django that can trim the off but models import user
and just with this one line especially it means that you have full access to the user class.
So everything we learned about creating forms or creating instances inside a model class for adding
a new row to a database that can all be done for the user as well.
You can see it's just user.
The objects that create user and all that knowledge that you know already can be done automatically
for you using the user class.
So this allows you to do a lot of things the way you already know how.
You're just swapping out your typical model for a user model.
I should point out you can also do things like change passwords.
So you can see here you could user the objects that get a particular user or a username and then set
their password to a new password.
This is a very manual way of doing this, but later on I'll show you that there's actually views to
tackle some of those issues.
And as you mentioned, there's also things like authenticating users.
So if you want, instead of doing authentication on the template side of things with user ID is authenticated,
you can actually do it on the python end of things.
So you can say user authenticate and then you could grab the username and password from something being
provided based on the view.
So you can use a lot of this code inside of use.
There's also things like permissions and authorizations, things that we've kind of talked about, but
now we can see it here in code.
For example, we can see my user groups and then you can set, add or remove, clear, etc., which
means you could actually grab my user ID doc groups.
It would return a list of all the groups that the user belongs to and you could start filtering things
based off that group.
So for example, you could say if the user belongs to the librarian group, essentially saying, if
librarian is in my user groups, then you could check, okay, show them the book.
If not, maybe you show them something else.
So there's a lot of functionality here that you just need to understand everything we've done previously
to then leverage the tools that the user built in model already provides for you.
Now that's a way of doing a lot of things manually, but luckily for us, there's also a bunch of what
are known as authentication views as well as built in forms.
So there's things that we've talked about like logging require a decorator and logging required mixing,
etc. A lot of that is manual.
But I should point out if you click here on authentication views, so click on that.
There's the views that we already discussed, the log in, log out, but there's also views for doing
things like a password change or a password reset or just resetting the account itself.
So there's lots of things you can do here that are already built in for you, and all you need to do
is import them from Django that contribute off import views as and they have off views.
So you have all these views available for you as class based views.
So keep in mind again, they're all class based.
Those means you can customize them with subclasses and then there's a bunch of authentication views.
So there's a log in view, there's a log out view, a password change view, and you can see some examples
here of how they're actually using this very similar to everything we've done previously.
Now, the other thing I want to point out is beyond the views that are built in.
So I would encourage you to check these out when you need something like a quick password reset view.
There are the built in forms, so there's a bunch of forms that you could set up inside a forms that
profile and then import into your views.
Or if you want, you can kind of skip the step of setting up as a separate forms up.
I file and use them directly, in your view?
If you're wondering how to use these built in forms, you basically use them in the exact same manner
that we discussed when using Django forms.
These are just filled out forms for you.
For example, an admin password change form or an authentication form or a password reset form, etc..
So let's go through an example of using one of these, which is the user creation form.
So right now, an administrator needs to log into the admin part of our website in order to create a
new user.
But obviously, creating a new user should probably be something that's simple enough to set it up in
a form that we can actually set up in a view.
And of course, there's going to be a form for that.
So there's a class user creation form, which is just a model form for creating a new user.
So as you might have expected, it's relying on all that knowledge that we've already learned about.
We already know about forms and we already know about model forms.
And we know that user is just a special model that's already built into Django, which means a user
creation form is just a model form for that user class.
Let's actually explore this in our code.
Okay, so here I am back inside it views that py where previously we added in log in required as decorator
and log in required mix.
And we also have our index page.
Remember, we did a lot of work on the template side of things to check if someone is logged in or logged
out.
And we can also see we have some May book create and book detailed views as well as this random view
to show you how to add in log in required.
What I want to do now is actually set up a view where we can sign up a new user.
It's kind of up to you how you want to set this up, if it should be under registration, for example,
or maybe you want it specific to catalog.
We'll show you how to set it up on your own custom path.
And what we're going to do is set up a sign up template.
So first off, let's actually create the view.
And as I mentioned, we can just grab the form itself by saying from Django, dot, contrib, dot off
the forms, import a user and we can see here there's a user creation form.
There's also things like a user change form.
So if you want to change information about an existing user and there's a bunch of those built in forms
like change, password, etc..
So there's also a bunch of views.
And remember, this is technically a form, it's not a view, so it's a model form for the user class.
Careful not to confuse those two.
Remember, the views are already set to go inside of used up by you.
Just use it like a detail view or a create view or model form based view.
And here we're actually just dealing with the form itself, which means we actually plug that in using
the form class attribute into a class based view.
So what's the best way and easiest way to use user creation form?
If I wanted to create a new user, I could manually create a little function based model form setup
thing that says, okay, user, the object that create new user, etc. and do it all manually.
But the user creation form can save a lot of time if we don't want to do it all manually.
And I can do it by piggybacking off create view.
So let me show you how that's done.
I'm going to.
Create a new class here.
So is the class based view and I'm going to call it my sign up view.
So we're going to call it sign up.
View and we intend to sign up people using this class.
And I'm actually going to inherit from creative you.
So it's a sign up view where we actually create.
And because I'm using create view, that means I can use the built in form class attribute and overwrite
it with the user creation form.
And then what I can do here is simply do what we already know, which is do things like set up a successful
URL.
So after they're actually create a user, I'm going to have them log in so I can do something like a
reverse lazy lookup for the login view.
So we can say log in here, but we have to make sure these names are starting to line up.
So this is actually based in catalog.
So let's check this out inside the URLs that py.
So I don't actually have anything for a log in view right now, so I need to be aware of the fact that
I may have issues if I actually want to link that using your URLs.
You'll see later on this actually will be okay for us because if you take a look at, for example,
index view, it's actually all built in.
You can see that I'm requesting your URL login so that naming convention by default because it's actually
built into Django at the top level, I don't actually have to worry about being able to look up reverse
lazy, so I'm going to save that.
And then the last thing going to do here is set up a template name to send back that form so you can
say template name.
Is equal to.
And if you want, you can actually build this inside of catalog.
It's kind of up to you.
Some people like putting an inside registration, which could also make sense.
But I just want you to keep in mind, you can basically pass it in wherever you want.
So signed up page HTML doesn't exist yet.
Let's go ahead and put that in.
But first, let's connect this view to yours.
I'll stop by.
So I'm going to come back to URLs, that pie and let's put in that sign up view.
So I'm gonna say path and let's have it be sign up for it slash.
And then we're going to say views dart sign up view and remember it's a class based view.
So I need a call as view and let's give it the name.
Sign up.
Okay.
So so far, while it may appear we're doing stuff that's super new to us, we're really just operating
on principles that we've already discussed before.
All we're doing here is using the create view.
It's a class based view that we already familiar with and we're just overwriting the form with the default
user creation form.
That saves us a lot of that work.
I remember because it's a class based view, it's essentially going to automatically pass in that form
for us and we can set up a successor are also once they register, takes them to that login page and
then we can say a template name catalog sign up three simple.
So let's set up that template.
So inside catalog, I'll create a new file.
And I'll call it sign up.
It's HTML.
There we go.
Now, remember, this is still a form, so I have to do a little bit of work here.
So let's go ahead and say H1 register new user here.
And then we'll say, create a form.
And then we'll see Methodists post.
So say method is equal to.
Post.
So we have our form and then we're just going to create a CSR token.
And then I'm going to pass in the form itself.
So it was not what I want to hear actually once.
Form.
And then I can say that as Pete and that with paragraph tags for each field, remember it's the username,
the email password, etc. and then finally I need a button here or I can also do an input.
Doesn't really matter.
So I can say input type.
And what I need is submit.
And then the value will be let's just say that's a classic sign up.
So it's clear what we're doing.
Um, and let's have this be one word here.
Sign up.
And there we have it.
Okay.
So we have that form set up for us views.
I'm going to say that right now.
And have we actually imported reverse lazy yet?
It looks like it's not the final.
Let's actually do that.
So we'll say from Django that your URLs imports and we should see now reverse lazy.
Okay there we go.
So nice of us.
That voice code was telling us to do that.
So what I've done so far is I imported the user creation form, set up this new class based view and
I'm thinking it to sign up, which should automatically link to the model for user.
So nothing too crazy that's going on.
I will have to manually type out the sign up link, but let's go ahead and check it out.
So in your URLs, I'm not going to go to catalog for slash sign up.
So let's go to the main page.
Let's also make sure we're not logged in.
So here I am, I'm already logged in.
We're going to log out.
So I'm no longer logged in and then I'm going to go to forward slash sign up.
This takes me to registering new user here.
You'll notice all these form stuff or form fields that's automatically done for you with the user creation
form.
I should note that there's a bunch of limitations.
Your password has to can't be too similar.
Um, must contain at least eight characters, etc..
So if you're getting a failure upon creating a new user, make sure your password is actually okay.
So let's create a username here.
I'm going to say my user two and the password will be let's say my password.
One, two, three.
Hopefully that's okay.
I think that should be okay.
You do have this automatic password confirmation.
So my.
Password 1 to 3 and let's try it out.
I'm going to click sign up.
And now I'm taking to the login page you can see by the URL.
So now I'm going to say my user to.
My password.
One, two, three.
Log in.
And welcome my user, too.
All right.
So pretty easy to do.
Basically, we leveraged all the ideas that we already understand and learned about in order to register
new users.
And there were many ways we could have done this.
I want to point out very specifically that this was not the only way we could have done this.
We could have manually set up a form based off what we already know how to do.
So if you really wanted to, you could review our lectures on Django on forms, and then you could have
come down to the topics of user authentication.
And then if you came here, could have gone to overview.
And eventually we can go to that page where it says, working with user objects and you could have done
this manually.
So you already know how to use Django to create instances of models manually by saying, okay, grab
the information from a custom form I created and then create that user.
Obviously this is such a common request that the user creation form object has already been created
for you by Django.
But I just want to point that out that you technically, just by viewing this box of code here, you
already know how to use all the skills to create a user.
It's just you have to remember that all you have to do is import user and then you can do that itself.
Way easier to use the user creation form though.
Okay, so that's it.
On how to let the user create users or register from their side of things instead of having to do it
all through the admin.
Thanks and I'll see you at the next lecture.