Creating Blog User Logins With Authentication - Django Blog #9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here Franco to be calm and in this video we'll look at user authentication for a blog with Django and Python I guess like I said in this video we're gonna look at user authentication but before we get started if you like this video I want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm which really helps out the channel and check out Kodi me comm where I have dozens of courses with hundreds of videos to teach you to code use coupon code youtube wanted to get $30 off membership that's all my courses videos and books for a one-time fee it just $49 which is insanely cheap okay in the last video we looked at ordering these posts and I went ahead today and added a new post so we can see that our ordering by the blog date worked so this is the latest one and so that's cool in this video we're gonna start to set up user authentication now this is probably gonna take a couple of videos because this is a little bit more intricate than some of the stuff we've done before it's still not hard but there's just a few more steps involved and what I mean by user authentication is we need to be able to log in to our blog and you know make post as it is right now anybody can just stumble along if we push this up onto the Internet you know right now it's just on our computer but eventually we'll push it up online and when we do that anybody could come along and just create a post here and we don't want that we just want us or somebody we trust a user to be able to make a blog post and same thing with editing right now anybody can edit any blog post anybody can delete any blog post yeah we got the delete button right so we that's no good we need some sort of authentication so that people can so they get to determine whether somebody is able has the you know authority I guess to make a post or delete a post or edit a post and we're gonna use the Django authentication system it's baked right into Django we can just sort of tap into it and so it's not that hard to do this but it's just like I said takes a few steps so we're gonna make this to where we can sign up as a new user remember way back at the beginning of this course we created a super user for the admin area right if you go to admin we get this admin area if we login we see there's users we have one user admin John elder so and we did all of this from the admin well now we're gonna do this so that we can sign up as a new user from the website itself so if you allow people to join your blog join your website and make blog post themselves they can do that you can use this these same steps for any website if you want to create any sort of membership website where you want users to be able to a lot that signup and then login you can use these same steps that we're gonna go in the next couple of videos to do that so it's very sort of a good thing to learn and we're gonna fly through it pretty quickly but it's not too difficult so let's just get started first things first we need to create a new app so if we look at our code up until now you know we've got just one app this the blog app and it's got all of our stuff in it and you know with Django everything is an app right your website is an app now we're gonna create a user authentication system and I want to create a separate app to handle all that stuff it's just sort of what you want to do I don't think you have to do that but it's a good idea to do that so let's head back over to our terminal ctrl C to break out of this and remember we're in C simple blog slash a blog because I'm terrible at naming things so if we LS we can see the manage pie files so that's how you know you're in the right spot make sure your virtual environment is turned on and we're good to go so now we just need to create a new app and we'll do this the same way we created the last app it's just Python managed up I start app and then we want to name it so what do we want to name this app we're gonna create members like members can sign up so I'm just gonna call this members and now we hit enter and boom that should work now I'm just gonna run the server again just to make sure it's it's running all right now head back over to our code and you'll notice we now have this members directory it has all the same files that are the blog directory had when they had started out but you'll notice it doesn't have a URLs PI file just like our the blog app didn't have a URL spy file so we need to create one of those so let's head over to members right click new file and then this just go ahead and save as and this will be urls dot pi and we can actually go to our old urls Wi-Fi if we want and just copy this whole thing and paste it into the new we don't need this thing here with all of our imported views because we don't have those views anymore and we don't need all these paths we're gonna create our own but for now this will work okay and you know any time you create a new app you need to add it to your settings that PI file just like we did when we created the the blog app at the beginning of this course so let's head over to a blog here and look at our settings and we could just come down here to installed apps and here's the blog app that we added earlier now we just want to add members and put your comment there at the end go ahead and save that and we close that now also in our main a blog directory we have our original URL spy file and we need to tweak that because when we added our the blog one we had to add a path to its urls dot pi file we need to do the same thing for a members app that we just created but it's gonna be a little bit different than than this one so what we need to do is just go path now this is gonna be we're gonna point this to our members directory Laurel and now we want to include and this is where it's a little bit different here we're not gonna point it right away to our members dot URLs as you might expect first we're gonna go Gengo dot contribu ILS and this is the oops we need a period there dot so the Django can trim off is the Django authentication system and it has a little package in there called URLs that will take care of all your urls for you so you need a login page a logout page a registration page it will handle those urls for you for the most part so we're gonna use those but now we also need to do just like we did here with our the blog app we're going to put that in here but instead of the blog it's gonna be members because that's the name of the app we just created so why are we doing two of them well we got actually put members here as well so why are we doing two of these URLs that point to the same place well because first the order of operation here is important we need this one listed for because Django will try and use these authentication URLs that come with this package and then if it sees something else besides those if we create some other type of page it needs to know where to go and in that case we want it to go to our members URL page which we just created right here and doesn't have anything in it just yet so okay we're good to go there so let's come back here and let's close this better save this first and let's close this new or this old URLs dot PI file make sure I save that one and okay so next we need some actual pages like a login page a registration page and stuff like that so that means templates now in the past we would put them in the let's see let's clean this up a little bit let's go to the blog and our templates directory in the blog but we're not in the blog now we're doing registration stuff we're doing authentication stuff so we need to a templates directory in our new members app right here so let's go to members right click and create new folder and we'll call this templates now inside here we want another one and I'll show you why in just a minute so let's create a new file or a new folder I mean and let's call this registration and so now in our members templates we now have this registration directory and let's create a couple of files in here let's create a new file and let's call this one file save as login dot HTML now let's create another one called new file file save as registration dot HTML that's pretty much all we need we need to be able to register if we want new people to sign up for our blog so that they can make blog posts and when they are registered we need them to be able to log in basically we'll make a link to where they can log out but we don't need a page for that right we just need a URL that logs them out but we do need a login page and a registration page so okay I'm gonna go to my I don't know article detail page mmm no we need a form so let's go to our add post page and our the blog directory so let's go templates and that was add post yeah so let's copy all of this text all this code and head back over to our login page and paste it in and here at the top for the title we just want login now this extends base will find our based at HTML file in our old the blog app so we can leave that the way is and then for our ad post this looks good and instead of post we want this to say login and instead up here instead of it saying add post we want this to say login but the rest of this should be fine and if you can hear some background noise I don't know if you can hear that or not but they're repaving my street on my blog so there's a little bit of noise back there I don't know if you can hear it or not but apologize if you do hear it but we're just gonna push on so alright I'm gonna copy all of this and head over to our new registration dot HTML file and let's paste that in and instead of saying login here let's say register and up here let's just say register and down here let's say register and that's really all we need to do we're gonna be using these same form as forms that we did back in the old app and these won't be styled right now but we can style them later if we want but for now this is fine okay so save this save this and go ahead and close these files we're pretty much done with them now let's head over to our views high file and you'll notice in our members directory we have a new views da PI file and it is completely empty because it should be it's a brand new app and up here at the top we need to import a bunch of things so we need from Django that views import generic use some generic views we need from Django contribs import now we need to create a user so we need the user creation form and just like before when we login or register we need to reverse lazy to wherever we want to redirect to so from Django dot URLs import reverse underscore lazy okay so now we need a class and what we're gonna create first is our registration form so let's go user register view and we're gonna pass in generic dot create view we could we can import that here from generic right and now we need a form class and this is going to be our user or creation well let me just copy this from right here did I make sure I spell it right user creation form right this will handle the actual form itself and then we need to point it to the template now these are very much like our old views right we designate the form class the template and the success URL right so that will be in registration slash register at HTML and that is just this templates slash registration it already knows to look in templates but we put it in this registration directory just to sort of keep it separate from this templates directory down here so it's not confusing to the two Django right and slash register dot HTML which is that file we just created right here okay so that looks good and then finally we need a success underscore URL and we want to send this when we want sinister reverse lazy and then login why well once we register for as a new user the first thing you're gonna want to do after you register is login so that you could start using the app so I mean we can send this to home if we wanted to but we'll just send it to login okay so that looks pretty good what else do we need here let's see I think that looks good now we go to our urls dot pi file now this is the brand-new one we just created doesn't have anything in it at all yet and up here at the top we just like we did before we need from dot views import whatever that view is we just created so we call it user user register view so we import that there and then our pattern is gonna be a path and of course we want this to go to register slash well not of course but that makes sense to me let's see register and this is going to be the user register view dot adds underscore view same sort of format we've done in our other views up high file right and then let's name this thing and let's call this register and comma okay so that's really the only URL we're gonna need and now we can use this register name to create a link so let's head it back to our old base pi file that'll be in our old templates directory here it is based off v dot pi and let's look at our nav bar code here we've got this add post link we also now want to register link so I'm just gonna copy all of this and paste it in and I'ma copy this again and instead of it saying add post let's call this register and we want this to point to register okay so let's save this and see if this is working so make sure our servers running head back to our website hit reload boom that seems to work if we click this register oh oh we have a problem that I spell template name wrong what did I do alright let's go back to our views dot PI file ah I did spell template name wrong template underscore name why didn't you tell me because when we look at our old views dot pi file for our old app we always use template name right template name double name tablet name it is Monday morning here in Vegas and so I just did that wrong all right save this head back over here and let's try this again boom reach all right so template does not exist members register hmm did I spell something wrong here hmm yes I'm all right so this should be registered HTML not registration okay so that works it is definitely Monday so all right hit reload boom we got register and we can actually create a username now very cool now if we try this well actually first let's create a link up here to log in real quick so let's head back to our base dot HTML and let's copy this now we haven't created a login URL yet have we but we don't need one because we can just type in login here and remember in our original URLs PI file let's see in a blog we created these Django contribu arel s login is one of those URLs so we can just reference it so let's go ahead and save that and come back here and hit reload and now we have a login link up here and if we try and log in remember using our old login we're gonna get an error because we haven't set up a redirection thing yet so let's do that real quick and that's super easy we just come back to our original settings that PI file well our only settings that by file and down here at the bottom we can just kind of tap down a little bit and all in capitals we just go login underscore re redirect underscore URL and we just want to set this equal to home now we need two of these so I'm gonna copy this and paste we need one for the login we also need one for logout so I'm just gonna go ahead and save this and alright that should work so let's head back over here give this another try login oops and I you notice I typed in the wrong password so we get some validation that's very cool give it another try boom and now we're logged in now you can't tell that we're logged in because we need to make now some changes to the code to reflect that but we can now also register so let's create a new one let's create Bob and just create a password and register and boom that works now it sends us to our login page here and if we wanted to we could log in there but let's go look at our admin area and we've already logged in and we can click on users and now we have this new user here so we have our old one and we also now have Bob alright so that's pretty cool not Bob doesn't have any of these other things listed but he can log in and log out and I'm just gonna click the Bob here and delete because we really don't need that I just wanted to show you that that works so alright we are now done like that's it as far as setting up the user authentication now in order for us to sort of use that we need to tweak our code so for instance when we click this add post page we need to recreate some logic that says hey is the person logged in if so show this page if not don't write and same thing with these things these links if the users logged in show the edit link if not don't and I think we'll start to look at that in the next video so just getting a little bit long so we'll do that in the next video so that's all for this video if you liked it be sure to smash the like button below subscribe to that channel give me a thumbs up for the YouTube algorithm and check out coding Meachem or you can use coupon code youtube 1 you get $30 off membership say pages $49 to access all my courses over 40 courses hundreds of videos and the PDFs of all my best-selling coding books join over a hundred thousand students learn to code just like you my name is John elder from Cody be calm and we'll see in the next video
Info
Channel: Codemy.com
Views: 21,312
Rating: 4.9722991 out of 5
Keywords: django user authentication tutorial, django user authentication and authorization, django user authentication login, django user authentication, django user auth, django.contrib.auth tutorial, django.contrib.auth, django blog login, django blog registration, django blog register, python django user authentication, build a user authentication web app with python and django, python djanog user, python django user registration, python django user profile, djanog tutorial
Id: mpfHDSmqHds
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Mon Apr 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.