User Email Registration. Django rest framework project tutorial. [3]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright guys welcome back so in this one we will go ahead and create our registration endpoint and make sure you can register users or also propose that if you haven't subscribe to this channel I highly recommend you subscribe just so you can keep in interloop as we post new videos so now in a view sleep UI we need to create our endpoint so we are going to be using class this views so right here I'm going to create a class just going to be called registration register view so this one is actually going to be inheriting from generic behavior so let me bring in generic CM so from rest framework input generics so now we can use generics in Eric's but this one is gonna be generic API view and since you guys will be just saying they are going to be making a post request so we need 200 post requests the DEF post so first it himself and then they request so in here now we need to get like the data that they use a sensor and set up a variable this one is gonna be holding user request data alright so once we have this data then we send it to our serializer class and Robin so I'm going to first create the series a class so I'm gonna creative for your final course early risers good py so in here you need to set up a say Araiza class so I'm gonna traits class register serializer this is gonna be inheriting from Sri Rises when you bring them in yet Congress framework import severe rises so this is going to inherit from Sri raises that model serializer so now in here we can define our so you raise a field so one of them is gonna be the password because yeah we want to lead us to have passwords so password this one will be three rises shattered so now we can define our validation so here we thank you for steak max think this should be h68 and then we can define our mini lengths of course to mean underscore link so now this one can be like six so if it passwords can be only can be at least six characters or so once we have that then when it comes to passwords we reach I don't want to be sending them to the to the front end so you can define a right on me so right under spawn only take this one to true so that's they are not sent them for reading they are not they are not sent for reading back to the user and we can only write them so once we define that then we need to define no class meta so class meta so in here now we can define our model so model will be your user so let's bring it in go to my doors go to user all right so now we can define our fields so food yeah this will be at least this is going to contain the things we want to expose so one is will be the email the next one will be the username and the next one will be the password alright so since we say that the password is gonna be right for me it means that whenever we send back responses regarding this model you'll be saying there's any one email you know the password alright so once you have that now we need to define the very date method so gonna call it did forget so in here now we can vary date take user information from here so you know of you once we get the data then we need changes to this erisa so since you created a syriza even if you are not then we can bring it in so from rotary raises input register so your Raisa so here they set up to say create a class severe Liza underscore class this is gonna be all registered a raise so once we have that now we can send this data to eat so we can do that by doing so you Raisa so can do sir so you write a class and then we pass the data so you can do data equals user yeah so this now should be able to send this data to the power Surya Raisa and now we can go ahead and forget it if I will do that for it to first make sure it violates it we need to run sir your razor is varied and then yeah you can put raise exceptions equal to true so when it's something that not valid it should tell us so once we have that now we can wrestle ERISA let's save okay so this is going to be running a function called a method code validate and this is going to be running if a method called create so let's go ahead and finish them out so right here this check for the email so here we can do email because I trust put get I want to get the email and then I'll put a fallback where you just so it doesn't crash so do the same thing for the username alright so this one will be getting username of course so once we have these two now we can do some validations let's say one occasion you have on their fan mail characters we can do like if note username which is arnhem and then we want to raise an error if it's not alphanumeric well this should be is a name like that no underscores alright so once we have this now we want to raise an error so you can do so here erase Syria raises dot the radiation era so here now you can say something like the username should only contain alphanumeric characters so what we and once we have that now we can return our attributes right so once now we do that now we need to define and create so you create actually gets cold when we say when we say use surveys about save stress define it create so now here what we need to do is basically create a user so here we want to do return user with objects then we call create user and now we give it our our data so a little actually be invalidated data so now we can pass it in here all right so make sure you pass this although it's gonna be sending only the username all right so once we have this now let's go back to a view and complete it up so after the save once data is created it's going to be returning as the user we can have something like user data so this is going to be from serialize encoded data okay so once we have the data now we can tell you that the account was created so for us to do that we can return a response returned not returning return response we're gonna put that on now responsible for should be taking the data which now will be user data and then we make sure first imported so from rest remark with response input response okay so can also bring in status from rest framework input actually I can input it here alright so now we'll be sending back the user data and then we want to send them a status of 201 so status equals let us the HTTP to run created all right so this should do standing back there is with the user and then this should be handling the errors in the by date so let's take a look at the very data game so basically what we are doing is we are checking only the user name for now let's first do that so once we have defined our view we need to register it in our URL so I'm gonna create a URL to a file so in here we define a new RS button and now I'm going to input path from the jungle URLs jungle with URLs input path so you know we can define our path but I want to first bring in our view so from good use imports record is register view alright so now we can define a path so want to see whenever I use a strange access register and then we want to give them a view so and now do register here you want to code as view then we need to give you the names and M equals register all right so once you have this I'm sure we don't need this slash we actually need it at the end ok so once we have this now we need to hook this one up with our main application routing so in the main application new REO hpy right here I'm going to import include so down here I'm going to now have all the they requested to go to slash authentic Earth yeah that good / off and then I want all of them to be routed to the top education app so here we can use include and now we can say we want all of them to go to the 10th occasion it's euros all right so once we have done this now let's save and actually test out to see what we have all right so I'm going to go back to my terminal and then run the application again brand server ok so we need to actually run migration since we set up a model so python manager py want migrations that greater user user migration and then we need to migrate to run migrate okay so that's runs in my Krishna so now let's run but the server okay so this brings back the server so now I'm going to try accessing it and now you can see that the index route has actually changed but you have now some other route so this one is slash old so I'm going to bring up my API testing clients so I'm going to bring up postman and of course we'll set up the recommendation that we can use but it will be a bit later so you bring a postman so here I'm going to create another collection this one is gonna be cold like income expenses API and we'll work from there alright so so now I can add a request this one is gonna be produced at save so now this now that you have a URL let me actually get it it's gonna be the main application so Ashworth slash register alright so this when you say do you have to make a post request let's try it and then you see we get own it - yeah so so right now when we we make a request to the API you can see that we get our error so the first one is same email email is required user name is an image you Craig okay so let's first go ahead and supply this because you can see they're actually preventing us from creating an account so in row good JSON and then here now we can start sending in this alright so let me actually clear them out and then we want to test out the validation see if we they actually work fine so I'm not going to change anything but I'm gonna make another another request actually validating the email since we use the email field alright so if I use the correct email let's say I use this one Gmail respond try sending you can see that now we get the error we defined about the about the username so I'm not going to be showing you how to fix this issue I'm sure we can do that one real quick so you can see the key is nine filled areas so for us to be able to change this we kind of try to find another key in the rest framework so right here in settings @py so here now we need to define all our all our configurations for the rest room so I'm gonna set up another key the rest framework and this one is going to be a dictionary so the first thing we do to set up the key to use for this one we are going to need set up none under start build errors key and then this one is gonna be Eric alright so this should be a three string so if we come back surveys of this check oh it's a box rerun it again you can see that now it changes to era which actually makes sense anyways so once we are done with that now let's try to send a request with varied user credentials so I'm gonna make this one username then let me supply a password so s is a paper so that's less than six characters so same that because that we get the idea that error but for us to be able to now register of course we need a valid password let's try that all right so you can see that we get who actually gets an error object instance so if we look at our create method you consider two create but then if we look at our models that do I because that creates use actually we are not resigning the user so Pia is the return to return user alright so let's try again so you can see that these ones are taken let's just use like one here one here so st. and yeah so you can see not not that now we get it 2:01 created and then we can see our user being given back to us so this will do it for this video if you enjoyed it please give it a thumbs up subscribe to the channel so you can keep in the loop as we post new videos I'll see you in the next one we will be sending a an account verifcation in
Info
Channel: Cryce Truly
Views: 23,493
Rating: undefined out of 5
Keywords:
Id: u0qpTOmXvvs
Channel Id: undefined
Length: 16min 18sec (978 seconds)
Published: Wed Jun 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.