Change User Password Page - Django Blog #25

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from Kota me.com and in this video I'm going to show you how to create the change password page for our blog with Django and Python hi guys in this video we're gonna work on this change password page but before you start it if you like this video 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 and check out code Meachem where I've dozens of courses with hundreds of videos that teach you to code use coupon code youtube one to get thirty dollars on membership that's all my courses videos and books for one-time fee it just $49 which is insanely cheap okay so we've got our profile page all worked out we did that in the last video made it all bootstrap a fied in this video we want to look at this link here that allows us to change the password and we can see right away it doesn't work so let's head over to our code and we're gonna be in our members directory this is all authentication stuff so what we want to do is go to our URL spy file and it's the one inside of our members directory make sure that's the right one and we need to import some stuff so let's go from Jango dot Kant rib dot off we want to import views as auth underscore views and this will allow us to use some of the views that come with the authentication system that comes with Django right so let's create a path and we want to point this to password don't forget the little slash there why password well because when we come back over here and look we're pointing our URL to this password right here so that's the URL we want to create now we're using these off views so we can copy this and what we want to do is off view dot password change view and this is dot as view as we've been doing with all our other ones and put your comment at the end now this will work strictly speaking we'll go ahead and save this head back over to our website and hit reload boom we get this Django administration looking page and this will work we don't have to be logged in as it in man we just have to be logged in as a specific user and that user can change their password using this page and like I said this works right so it's not great because we've got a whole theme going on here and this doesn't look at all like our website so how do we change that well it's actually pretty easy and back over to our code and we can just pass in an argument into this function our password change view as view we can give this a template name and set this equal to let's point this to registration / change - password dot HTML and why registration because in our templates all of our template files in this directory are also in a registration directory so let's go ahead and copy this head over to registration right click new file and let's go file save as and call this change - password calling a change - password because that's the gender of documentation recommends but you can name it anything you want now while we're here let's let's go to our login and let's just copy all this code and paste it into the change password and we want to change this to change password and we want this thing to say change password and let's see the button should probably say a change password okay so now we can go ahead and save this so come back over here hit reload boom and now we have our change password form so this will absolutely work but there's a couple of things going on here one this doesn't look great - if we change our password and then click Change Password it redirects us to this Django administration page right now you're supposed to be able to just like we did when we passed in this template name you're supposed to also be able to pass in a success URL but I find it to be a little wonky and it doesn't always work so instead we're gonna change this around a little bit instead of using this off views we're gonna create our own view in our views dot pi file so let's go ahead and do that now let's just come up here and let's create a new class and let's call this password and I'm gonna make this plural plus passwords change view and we want to pass in this let's see head back over to URL spy this password change view from our off views right so let's go ahead and pass that in like that now in order to actually use this we have to import it up here so let's come right underneath here this is gonna be from jingo duckin trib dot auth dot views instead of dot forms right here right we just want to import that password change view so now we can use this inside of here okay okay so the first thing we want to do let's give this a form underscore class and that's going to equal our password change form right and this is in Django contribute import it so let's just paste that in there right okay now underneath here let's just come down here and for now grab this success URL reverse lazy and send it back to like our home page or something so now if we save this we need to actually import this thing into our URLs dot Pi file so let's copy this and head back over to our URLs dot PI file and actually I'm gonna paste that in here for now I'm gonna comment this out just so that's still there so you guys can see it later but what we really want to do is change this around a little bit using this guy right here that we just created so instead of auth views pass or change views as view okay we want to use that passwords with plural s change view that we just created on our view stop PI file right here alright so save this head back over here we also now need to import this passwords change view as view up here where we import all of our views right so but comma boom passwords change view okay so save this that looks good that looks good that looks good we probably don't need this anymore or we could pass this on the view but let's leave it for now and save this and head back over here and click reload make sure this still work so I'm gonna click back and click on this thing make sure that's the work so our old password let's try and change this if this n hopefully if this works it will redirect us back to our home page and it did and that's cool so that's that's good that works but maybe you want to redirect it to a specific page or something well you could do that in the same way that you always redirect things so for instance let's head back over here and let's just create let's create a new path and let's call this changed or let's just call this password underscore success okay and we want to put this in views dot password underscore success and we want to give this a name equals password underscore success sure and put your comma at the end and we're just gonna create an old time you know a functional view like we've done in the past sometimes when we're lazy so we want to come up here and from everything import views okay so let's copy this save this file head over to reviews that PI file and let's just really quickly let's go to fine and let's call this that I'm gonna pass in a request and then we want to return render return render or request and let's just point this to registration / pest for success HTML and we also always want to pass our contacts dictionaries and okay that's fine now let's create this guy real quick and so let's head over to our registration templates click new file files save as and it's call this password success dot HTML save this and just open any of these things login page is fine copy all of this paste it into this page in here let's go password updated and down here let's go your password was changed I don't know successfully success fully there we go thought that thought and let's just get rid of all the rest of this stuff so go ahead and save this so we created a very quick little view password success it just points to this page which just says password was changed successfully now we hadn't need to head over to our original passwords change view here and change this let's comment this out and create a new one to whatever we named this in our URLs that PI file so that was C underscore success so we can come back to our used up pie file paste that in there sure put your rotation marks okay so that should work so let's go ahead and save this head back over to the web page hit reload all right so let's go I've forgotten what our password is we've changed it so many times but now we click this it goes to this page your password just change successful and you can make anything you want on this page make it look whatever you want I'm just gonna do this really quickly as as this is and just leave it like that so alright that's pretty easy now the one last thing we can look at is bootstrap a fiying this form right so since we have our class-based view here for our passwords changed view we can easily change this form class like we have other times right so if we wanted to we could head over to our form stop pie file and I'm just gonna grab this guy right here copy em and let's just come down here and create a new one called what password changed form or password changing form and instead of a user creation form this is a password change form and we probably need to import this so it's head up to the top here and from Django could rip off forms past that end so password change form now what fields do we want these to be well I happen to know what they are but if you didn't you could come to this page right click and view the page source and just come down here we've done this in the past you know you find the form fields and here they are you just take off the ID parts at the top so all right at the front of it so each of these is the first one is old password the next one is new password one and then one after that is new password to just correspond to old password new password one and new password too so we can use those so this is gonna be old underscore password this one's gonna be new underscore password one and this one's going to be new underscore password too and we need to also pass these into this list down below so there's that one there's that one and finally there is that one and get rid of the rest of these so we just have those three things there and we're still using our user model so that's good now up here we don't want to use email fields or car fields what we want to use is well I suppose car field will work but here inside of it we're where we define our widgets we don't want to use input your email inputs we want to use password input and instead of text input also we want to use password input and same thing here password input and we're doing form control like before this will bootstrap afire forms but we can also give this a type because bootstrap deals with passwords in a special way on their forms and so we can put a come-on here and call type : password and we can just copy this and paste that there and paste that there okay I think that will do it okay so let's go and save this and now take note of what we called this thing we called a password changing form so we need to copy this and head over to our views dot PI file and now instead of this and let me comment this out so we have it in case we need to change it back or something so this is gonna be form class equals now we'll just pass in passwords for password changing form or saying hey use this form now and we also need to import that from forms right here just paste that in okay so save this save everything back here cross our fingers hit reload and boom now it's bootstrapping fide if we click this you see they they glow right if we do the wrong password we get some error messaging right one thing that's different is those those bullet points are gone and if we head back here and let's comment out this one and uncomment this one and save this and reload this you can see now let's go back to our edit profile click this again now you have these there and those disappear if you want to keep those you can always just copy them right so we can right click and click copy and then head back over to our let's see change password page and anywhere we want say below the button maybe we could just create an unordered list and close our unordered list and then just paste these in and then we can give each one of them an Li so there's this one this one this one and this one and each of these needs to be closed so copy this close that one that one and that one so we save this now if we head back over to our views by file and let's switch these back again to use our bootstrap F ID form back over here hit reload boom now these are there that's kind of smoosh it up against there so that doesn't look great so we can go back to our change password page again and you know just give it some line breaks or something save this reload boom there we go so let's try and try this with the bootstrap file let's go let's see that right your password changed successfully and everything works so that's the password page kind of convoluted a lot of moving parts but really easy a lot of stuff that we've already done in the past you know boots stratifying these forms we've done this a bunch of times already same process basically we just use password inputs instead of text inputs but other than that it's the exact same process a little bit different to create this page right using that let's look at this the ordered our URLs defy file go this auth thing that's different we've never done that before so that's a little bit different but in the end we didn't actually use this thing we still I guess probably do we even need to import this since we're not using views probably not so if we wanted to let's comment this out save this and make sure this still works and yeah it still works because we're actually not using that because we changed it to our own class-based view that we made there at the end which is just this guy the reason why we did that of course is so that we can more easily create a success URL and then we can also then change the form to this password changing form that lets us bootstrap fi it so that's all for this video if you liked 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 the channel out I really appreciate it guys and check out Co to me calm or you can use coupon code youtube wantin to get $30 off membership they pay just $49 to access all my courses we've got over 45 courses right now hundreds of videos and the PDFs of long my best-selling coding books join over a hundred thousand students learning to code just like you my name is John elder from Coto B comma we'll see in the next video
Info
Channel: Codemy.com
Views: 13,890
Rating: undefined out of 5
Keywords: django auth password, django authentication change password, django.contrib.auth.views, django.contrib.auth, django auth_views, passwordchangeview django, django passwordchangeview example, django passwordchangeform, django password change, how to change django admin password, django change password view, change password django user, python django authentication tutorial, python django authentication, python django user authentication, django authentication
Id: P6QHswl2PqE
Channel Id: undefined
Length: 18min 18sec (1098 seconds)
Published: Thu Jun 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.