Login With User Authentication - Django Wednesdays #21

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys john elder here from codingame.com and in this video we're gonna start to look at user authentication for django and python all right guys like i said in this video we're gonna start to look at user authentication before we get started if you like this video and see more like it be sure to smash like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out codeme.com we have dozens of courses with hundreds of videos to teach you to code use coupon code youtube1 to get 30 off memberships all my cursive videos and books 110 feet just 49 which is insanely cheap okay in this video we want to look at logging in and if you remember up until now we've been using the admin system here so we log in here we do all the stuff we can add users we have two users and men well actually we deleted one of them i guess we've only got one user but we can add users right here we can edit them we can remove them and that's great if you have a system administrator like me that wants to add every person manually but we generally don't want that in django or in any website you want people to be able to log in themselves you want them to sign in sign out update their own information edit their profiles do all the things and you want them to be able to do that from the website you don't really want them to be doing that back here in this admin section so we're going to start to look at setting up all this administration stuff for the next few videos now like i said this is going to take us a few videos there's a bunch of stuff here in this video we're going to set up the login so i'm going to we're just gonna keep assuming that we've used that administrative area already we've set up a user and now we're gonna build a web form where that user can log in from the website itself eventually like i said we'll set up the ability to register and edit profiles and all the stuff from the website but just for today we're just going to set up the login system and it's going to set the the stage for everything else and it's going to set the system up for everything else so head back over to our code i'm using the sublime text editor and the git bash terminal as always and as always you can find a link to the code in the pin comment section below as well as a link to the playlist with all the other django wednesday videos so check that out if you haven't already so what we need to do here you'll notice in here we've got our events app we've got our my club website app this is the main app we have when we started that's our project file right and that's all we've got now i want to set up another app inside of our project here just for authentication i'm going to call it the members app and it'll take care of all of our logging in logging out user registration all that good stuff so to do that we need to head over to the terminal and control c to break out of here this is where we've been running the server and i'm in my c my club my club website directory we've got the virtual environment turned on and now we just need to create a new app in our project and we did this once before when we created our events app we just go python manage.pi start app and then we just name this thing and i'm going to call this members because i don't know that makes sense to me okay now let's go python manage.pi run server to make sure our server's running again okay so now the first thing you do anytime you create an app well first let's come back here we've got this new members directory and it has all the things inside of it but the first thing you always do when you create a new app is to add it to your settings.pi file so let's come down here to our settings.pi file and we've got this list of installed apps i always forget to do this and it always messes me up we want to add members and always put my comma at the end so just make sure you do that first off if you forget this step nothing else is going to work and you're going to be banging your head going why isn't this working all my code is correct well you forgot to put it as your installed app and that throws everything off so now it's in there and we're good to go so the first thing well second thing i guess is go to our members and you'll notice just like when we created our events folder up here there is no urls.pi file in this new app so we always have to add one so let's go new file let's go file save as and i'm going to save this as urls.buy and we can open up our original urls.pi file down here and i'm just going to copy this and paste it in now we're going to have different paths obviously so we can take that out and we don't need this thing or include but we do need from dot import views as we usually do so if we look at our events.pi file our events urls.pi file same deal so okay so before we start creating urls and things there's one more thing we need to do in our original urls.pi file this is the one that gets started at the very beginning of our app and here's where we're pointing to our events urls pi file that pi file we've got a new app so we need a new path to point to that so i want this to point to members and then here we want to include and this is going to be members dot urls okay so that's the first thing we need to do we actually need to add another path to this too it's a little weird so again i'm going to call path and i want this to point to members wait we already did one for members oh we got to do another one because this member's app is going to deal with authentication and django has a user authentication system all bundled up into it and ready to use but we just have to reference it and we're going to use that authentication system to do most of the heavy lifting we're not going to reinvent the wheel here we're not gonna create our own login system and our own password system and all that stuff we're just gonna use the one in the authentication system that comes with django but we have to reference that here so to do that we wanna include and we wanna pass this as django dot contrib dot auth dot urls so this will allow us to use a bunch of urls that come with the authentication system the login the log out all session information and all kinds of good stuff so uh be sure to put the comma after that and so okay that looks good we're good to go there we go ahead and save this and close it so now let's create a webpage for logging in so let's see i'm going to close my events thing here starting to get a little confusing with all these things so i like to close them as you know we're using things so here we're in our members directory let's create a new folder and this is going to be templates and then inside of here let's create another folder and i'm going to call this authentication and this is where we're going to put all of our html files let's create a new file and i'm going to go file save as and let's call this login dot html and we can come back into our events app here and look at our template files and we can grab you know something from here it's going to go ahead and do that paste it in we want to reference our base file in the same way as we did before here instead of add event let's call this login and we can get rid of that we are going to have to have a form but it's not going to be that so i'll leave that off but we will have a form so we'll leave this here and get rid of this end if okay so that looks good let's go ahead and save that here's our login page so now let's come over to our views.pi file let me kind of minimize some of this inside of our members directory so there it is and it's brand new look at that there's nothing in it it's so adorable all right it was like a new brand new views.pi file but we want render and we also want to redirect in here so let's add that and now we also have to add the django authentication system to our views.pi file here so let's go from django.contrib dot auth for authentication we want to import authenticate we also want to import login and we also want to import log out and you know for good measure let's also from django.contrib let's import messages so whenever we log in or log out we want to flash a little message up that says login was successful or login was not successful or whatever so we'll use our messages we've already looked at that in prior videos in this playlist check that out if you haven't already so okay let's define a new function and i'm going to call this login underscore user and we want to pass in our request as always now you may want to call this login but up here we are importing login so we can't name this function login or else we'll get a conflict so i'm going to call it login user and for now let's return render and we want to pass in the request as always and this is going to point to authenticate slash login dot html and we want to pass in our context dictionary of nothing but okay so far so good so this authenticate slash login.html is pointing to our authentic oh we call this authentication let me rename this uh folder let's just call this authenticate there we go so this authenticate is pointing to this authenticate directory it has this login.html file so okay so good so far so good that looks correct let's go ahead and save this now let's head back over to our events directory and our templates and our nav bar and let's add a link to this guy so let's come down here and this is getting a little bit crowded so we're gonna have to do something about this eventually but for now let's just name this guy login and we want to point this to login now we don't have this path yet so let's create that minimize our events directory and come into our members directory again go to our new urls.pi file and let's create this guy so inside of here let's go path and this is going to be login underscore user we want to point this to views dot login underscore user and we want this name to be let's just call this login or comma there be sure you've imported your views here so that we can use our views and okay that looks good so let's head back over to the website hit reload now we've got this login and when we do we go to the login page you'll notice it's pointed at members slash login underscore user so that's cool we've got a submit button it doesn't actually do anything but so far so good so let's put a little forum on here and play around with this so i'm going to go to get bootstrap now we've done forms different ways throughout this series and you know we've created a form in our forms.pi file and we've used that you can certainly do that here i'm going to do this a little bit differently just so you can sort of see how this works with the authentication system in a more hands-on sort of way let's come down here and let's grab this guy here we've got email address and password i'm just going to copy all of this and let's head back over to our code and go to our login.html page and here we've got the beginnings of this thing so i'm just going to kind of paste this in and get rid of that form thing we can get rid of this button thing it's got a check box thing we don't need that so let's get rid of that and let's come up here and this email address into the email address let's have this say user name and it's instead of it being a type of email let's give this a type of text we don't need it to be an email address right now and okay we can get rid of this thing i'm going through this pretty quick but we you know whatever and this one will say passwords now each of these input boxes needs a name so be sure we have our csrf token and here's our username one where it says id equals i'm going to change this to name equals and inside of here i'm going to call this username and you'll see why in just a second same thing down here with the password one let's find it instead of id let's give it a name of password okay so let's come back over here hit reload see okay this doesn't look great we'll make it look better later but for now at least we've got a form here and if we type stuff here it's a password field so that's good so okay now head over here and if you go to google and just type in django authentication see the first thing that pops up is this thing right here and this documentation is great there's all kinds of good stuff you're going to want to read through and play around with you notice here's where we're calling the django contrib auth if we look in our views.pi file that's this django could trim off that we imported that allows us that allows us to use all the authentication stuff that comes with django and that's good fun if we come down here to let's see authentication in web requests that's what we want we want to log in from the website we want to authenticate in a web request right so it shows us exactly how to do that in our view we just pass in this stuff so let's set that up let's head back over here and let's go to our view and here it is and so before we do all of that we need to determine whether or not a person filled out the form on that page or if they just went to the page if they filled out the form we want to do this stuff if they just went to the page we just want to show the page so we can do a quick if statement to see so let's go if request dot method equals post meaning if they posted something do something else we want to do this right we want to render the page so this is basically hey if they go to the web page just show the web page if they go to the web page and fill out the form then do stuff well what do we want to do we want to do the stuff from this page specifically this stuff i'm just going to copy all this bring it back over here and paste it in now you're going to want to come through here and clean this up so that the indentation is correct as you can see by default it's not when you copy and paste stuff like this so make sure you do that there we go almost done okay so what we've got here is we're saying hey if they fill out the form then grab the username that's the request.post username and the password that's request.postpassword and we get this username and password because back on the page remember we we gave these a name where did it go here name of username and name of password so it's passing those names into our views.pi file and then right here username and password we're taking whatever we posted in the form and assigning it to these two variables now we can look up those variables and see if they're correct by calling this authentication function and this comes from django.contrib.off and we're going to pass in a request we're going to pass in our username and our password right so okay then once that's happened we can say hey if the user is not none then log them in and this is where we log them in right here with this thing right we're sending their request and a user and once that when that happens we need to redirect to a success page so let's return redirect and we just want to send them to the home page so i'll just pass it home right now we can use redirect because up here we imported redirect right so remember that else we want to return somewhere else so where do we want to return here well let's return them to the login page but let's pass in a message that says hey there was an error right so we can do that right here by going messages dot success ironically and we want to pass in the request and then uh there was an error logging in try again okay now that should work now the error message would get flashed up on our login screen because our login page has our base that html file and if we go back to our events and our let's see find our base.html file you'll remember way back when we set this up we added where did it go you know what we did not add messages uh-oh how did we not add messages well we're gonna have to add messages in just a second here but let's test this out first to see if at least work so head back over here and i'm just going to type in some stuff click submit uh-oh i'm getting an error invalid syntax line 18 of our views.pi file so what do we do line 18 views.pi ah right here this stuff needs to be inside this if statement okay that's right all right so let's head back here let's reload this page now let's not resend it let's just come back here start over click this some gibberish more gibberish click this it returns back to the login screen so let's actually log in with our login username if it's successful boom it goes back to the home page so all right that looks like it works now we still need well frozen to make this look better we'll do that later we also still need the messages thing so let's do that real quick first though let's go to our login page and let's get rid of all these line breaks that looks a little weird save that come back all right that's a little bit better we'll play with it later so but very quickly let's create the messages system let's see in our base.html file down here in our container let's go if messages and let's end if and then inside of here let's go for message in messages and then we want to end our four as well and then inside of here we can just you know type in the message now this doesn't look great so let's head back over to bootstrap real quick well first let's test this out to see if it works so save this head back over here to the site uh let's just put some boom there was an error log in try again okay so there's the message but if we go back to bootstrap and type and click on components and alerts we get this alert thing come down here to find the one with the little x on it there we go we like this one so we let's copy this head back over here and right here paste all that in and then copy this little message thing and instead where it says holy guacamole let's put our message okay so tab this over a little bit something like that okay so let's save this head back over here give this one more try uh nonsense nonsense bloom there was an error logging in try again all righty let's go to our admin page and log out so now if we go to our slash admin page it asks us to log back in so now let's go back to the website and try logging in gibberish gibberish oh there was an error try again so then admin type in the password boom it redirects to the home page now if we go to the admin section it doesn't ask us to log in because we're logged in which shows us that this whole thing works so we went through that very very quickly and like i said we're going to make this look better in the future and we did this form thing a little different than we've done other forms but we're using the login authentication system so that's why we do that and you know it's pretty easy we're just looking at two things so we can create our own little form for that you can use the forms.pi file for this and maybe we will if people are interested but that's how you log in so like i said we're going to set up a whole authentication system where people can sign up sign out edit their profiles do all the things and then we can give permission to people throughout the site where oh you can update this venue because you're logged in or you can't update this venue because you're not logged in or you can add this event because you're logged in or you can't add an event because you're not logged in and all the good stuff so we'll get into that in the next few videos uh in this video i just wanted to sort of introduce you to the django contrib auth system we've got our let's see new members app installed it's got a bunch of stuff we can now at least log in from the website and we're moving right along so that's all for this video if you'd like to be sure to smash like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out codeme.com where you can use coupon code youtube1 to get 30 off memberships they pay just 49. access all my courses over 47 courses hundreds of videos in the pds of all my best-selling coding books joined over 150 000 students learning to code just like you my name is john elder from cody.com and i'll see you in the next video
Info
Channel: Codemy.com
Views: 7,881
Rating: undefined out of 5
Keywords: django login and registration, django login page, django login form, django login authentication tutorial, django login validation, django login form bootstrap, django login system tutorial, django login and logout, login with django, codemy.com, john elder, john elder django, django tutorial #21, django wednesday, django wednesday #21, django authentication, django tutorial, django tutorial for beginners, django login tutorial, django user authentication
Id: CTrVDi3tt8o
Channel Id: undefined
Length: 21min 24sec (1284 seconds)
Published: Wed Jun 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.