Register a New User (Django Rest framework)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the next step in development is user registration so what do we need to register users from our REST API well we need their email we need their username and then we need to password so we need their password and then to confirm that password and so basically we want to be able to do this with postman we want to be able to send a post request to our IP which is currently our development environment is this right here so I'm just copying that and I'm pasting it in so it might look something like API account slash register that's what we expect the URL to be once we're done building it of course I haven't built that yet so we we still need to do that but we're gonna also need some body parameters so we need the email we'll need the username and we'll need to password so it'll be password password number one and then either something like confirm password so confirm underscore password or just password number two or password two which is what we're going to be doing so we need to build the serializer for this we need to build the view for this and then we need to add this two URLs pi all right so staying consistent with what we've done before I'm going to go into the account app and I'm going to create a new folder this is going to be called API just like we did in blog we have that new API folder for all of like the REST API stuff and inside inside API I need to first create a Python and knit file so underscore underscore and knit underscore underscore py nothing needs to go in here it just needs to go inside of the directory and I'm going to create three new files one will be serialized serializers PI one will be it'll be views PI and then the third one is going to be URLs PI so the exact same kind of thing that we did in the blogging API directory here we have a knit serializers urls and views and now we will do the same sort of process we're going to start by building the serializer then build the view and then build the URL so let's go into the serializer file for the blog app and I'm actually going to copy this whole thing because a lot of it's going to be very similar so I'm pasting that into the new serializers file and we need to change a few things so from Account models I want to import account this is going to be registration serializer so registration serializer once again we are going to use a model serializer since it comes kind of with a bunch of stuff prepackaged to make our lives a little easier the only thing that's different is going to be this new password field so I need to add a password field up here because it's password number two is not part of the account model so remember we're we're modeling it the serializer after that after a certain after a certain object in our Django on our Django website but of course it doesn't have a secondary password field so we need to create that manually so I'm gonna go serializers character field character field style equals and this is some this is some extra styling that I'm adding because it's a password field I need to set an input type of password so password that way the field will be hidden from the user so when it get it gets passed through the request it's going to be like an encrypted field so people can't just read that obviously and it's going to be a write only field equal to true that way people can't read it like I said now let's go down to our fields so we need the email we need the username we need password number one so that's just going to be password and we need password number two so let's delete that and password too so that's it for our fields now the next step is also something different from the previous serializer I need to create a extra keyword argument field here so extra key keyword args and this is going to be password : and I want to do the same thing that I did up here so I'm telling it that this is a right only field so right only and set that equal to true and again this is for security we don't want people to be able to read this field or this field when it get gets passed through the request to the server so we're saying right only to hide the password field and write only here to hide the password number to field now the last step in our serializer is something again I need to fix all this spacing so I'm sure you already know this about Django by now but if you different or it's actually a Python thing if you have indifferent spacing leading up to the different lines in it in something that you're declaring this is a problem so these are actually so if I press delete you can see there's a whole bunch of spaces that get delete so it's actually the spacebar but here's a tab so they all need to match so I'm gonna I'm gonna highlight all of these things I'm gonna tab them in by pressing shift tab now I'm gonna fix the indentation so I'm tabbing these in everything is going to have tabs that should be good so now if I highlight this all of the whitespace is the same it's all tabs so now now as I was saying the last thing we're gonna do that's different from this serializer compared to the other serializer that we built is we're gonna override one of its methods and we need to override one of its methods because before we save this new user that were registered registering to the server we need to make sure that the two passwords match so password number two and password number one so I'm gonna define a new method we're overriding the save method and I want to get the account first of all so I'm creating a new account so a new new user account I'm going to say the email is equal to self validated data so remember if you're working with Django forms you're working with validated data it's very similar with serializers it's the same sort of thing that's why I said in one of the previous videos to think of serializers sort of like Django forms they're very similar it's kind of like the form for the rest API so once basically once actually I'll show you a comparison so if we go into blog views and we go down to let's say the update blog view so remember with the serializer we check to see if it's valid and then we saved it so this is the same kind of thing that we're gonna do when we're registering a user we're going to check that it's valid and then we're going to save it so this validated data becomes available after you check if it's valid and then you're going to save so that's how I'm getting this validated data if you don't check that it's valid then you aren't able to get the validated data so just keep that in mind and I'll remind you again when we take a look at the view and we actually built the view now let's get the passwords so cell value data data again this is going to be password at number 110 let's go to the next line and password number two is going to be self validated data password two whoops okay so now that we have the password number one password number two we want to say if password number one does not equal password number two then we know we have a problem and we can raise a serializer error so serializers dot validation error and I can pass what I want that validation error to say so I'm going to say it's going to be on the password field so this is it's going to return some JSON data basically I'll pull up a notepad file just so you can see what it's going to look like you can see kind of a demonstration so this serializer I'll type it out and then I'll I'll show you what it's going to look like so passwords must match is what it will say with a period so what's that going to look like if the passwords don't match it will return a JSON object through the response and it'll say password pass words must match that's what it will look like and that's that's what this validation error is right here so just kind of a heads up for what that's going to look like but we'll test all that later so you'll see it actually in action now that we have the password and we know that it is they are the same we can do account dot set password set that to the first password an account dot save it's very important that you call a count dot save if you are overriding the save method because otherwise it won't say because right if we call save in our view this method is going to get called and the the actual save method will not get called it and won't get saved to the database so very important that you do that and then I just want to return the account so there we go that is our registration serializer now we're ready to move on to the view so we're going into views dot pi for the account API class and I'm gonna go into the blog views and I'm gonna copy a bunch of things a lot of a lot of this is gonna be similar so I'm copying these three imports the status response and API view I'm gonna paste those above and I need to import so from account API dot serializers I want to import that registration serializer that we just built and now we're ready to build that view so just like the other views that we built in the blogging API section we want to annotate this with at API view to restrict the type of request can be made on this view this is going to be a post request now I want to define registration view it's going to take in the request and now I can check for if the request equals post again I don't think you have to do this I just it's just kind of a habit of mine I just do it because technically you're checking for the post request up here in the API view annotation but you know I do it anyway it's not going to hurt so let's just let's just write it out next I'm going to define the serializer so serializer equals registration serializer the data for the serializer equals request data I'm going to define a data object that we're going to return to the view once everything's done just like we did before and next I'm going to check if the serializer is valid so if serializer is valid so now if it's valid remember we have access to this validated data so remember just checking if it's valid that means we have access to the validated data just like with Django forms now now I want to get the account so I'm doing serializer dot Save remember that's gonna call this over I did save method that we just built that will confirm that the passwords match if they do match it will set the password it will save that new account to the database and then it's going to return that account so that's how I'm getting access to that account object right there now I want to return a response to the user letting them know what happened so I'm gonna do data response equals you know a successfully registered a new user and then I can actually copy that line because that'll make things a little easier this one's going to be email and it can return the user's email so I can just do a count email and I can copy that line again I can return their username do a count dot u username and obviously I don't want to return their password so I'm going to stop there otherwise I want to do data equals serializer dot errors so this this will return that that error here if it was raised so if there was a validation error it'll say passwords must must match it'll get set to the data variable here and then I will return that response and pass the data so there's our registration creating the serializer checking if it's valid saving it which we'll call the override it save method which will check to see if the passwords match if they match will return this successful response if they don't then it will return the errors and it will return it through the response object or the response function so now let's go into URLs DUP hi inside of our API view so from Jango URLs import path and I could have copied this from the other URLs file but there's not really that much to write in so I'm just gonna write it out from account API dot views I want to import some stuff I just have one view right now but there's gonna be more so registration view is the first one and now I want to set the app name we must do this if we're extending the URLs file now URL patterns equals square brackets and the first one is path this will be register I'll do registration view and the name is equal to whoops not app name just name equal to register so that should be good I could add a comma there and the last step now is going to be going into the main URLs file so going into my site and telling it to include these new URLs so I'm copying that under my rest framework URL section here this is going to be API slash account this will be account API dot URLs and this will be account underscore API and that should be good I'm pressing ctrl s gonna bring up my development server actually I don't need the development server I need postman so we're going to postman we have the IP here we have slash API slash account slash register now let's enter some some new stuff so actually I will bring up the development server and just show you that there is not I'm gonna be registering a new user so let's go to admin check the accounts that are here there's just Jessica Mitch and Mitchell Tavian at gmail.com so let's create a test email let's do like test email at Tavian com the username will be testing email password will be password and the second password will also be password I don't think that it will let me register actually that's let's check to see what it looks like when you try to register passwords that don't match so let's do that passwords must must match that's good let's try it with that password successfully registered a new user cool so let's see what happens when we try to register a user that is already part of the part of the database or in the database so there's an email that already exists account with this email already exists account with this username already exists that shouldn't have printed but it it is supposed to print the email so that's fine let's try with a different email so an email it doesn't exist but a username that does exist account with that username already exists cool so it looks like everything is working as it's supposed to we can just double check to make sure that user was registered and there it is right there test email so everything's working as it's supposed to all right so everything at this point is working as we expect we're able to register new users now we're ready to move on to generating authentication tokens because remember later on in this course that's how we're gonna authenticate users when other technologies try to interact with the website they need an unique authentication token that they can attach to the header of the request so we need a way to generate those tokens so that's what we're going to work on in the next video you
Info
Channel: CodingWithMitch
Views: 58,240
Rating: 4.8229008 out of 5
Keywords: django rest-framework, django rest framework, django rest framework tutorial, django rest api, django rest registration, django rest register user, django rest framework register user, django rest user registration, django rest user authentication, django rest user registration api
Id: _OhF6FEdIao
Channel Id: undefined
Length: 15min 4sec (904 seconds)
Published: Thu Jul 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.