WEBVTT 00:00.420 --> 00:05.910 So in this lecture, let's add the ability for someone to be able to sign up for a user account via 00:06.000 --> 00:11.280 our API in the first place, that will have to go is to the your Urals and say, you know what, your 00:11.280 --> 00:12.990 l do we want someone to sign up with. 00:13.590 --> 00:18.300 So let's say, you know, they've come to the API, you know, slash API. 00:18.300 --> 00:19.120 What are we going to use? 00:19.140 --> 00:20.730 I think let's keep it really simple. 00:20.730 --> 00:21.870 Let's just use sign up. 00:21.930 --> 00:25.620 So I'm going to say path and then I provide in here. 00:25.680 --> 00:27.810 I want to sign up. 00:29.340 --> 00:29.790 All right. 00:30.270 --> 00:32.250 And then as far as what I'm pointing to. 00:32.730 --> 00:38.730 I'm going to be using the views, but in this particular case, I'm going to be using a function based 00:38.730 --> 00:39.090 view. 00:39.630 --> 00:41.070 So I haven't created yet. 00:41.100 --> 00:43.080 But the name that I'm going to use there is sign up. 00:43.080 --> 00:47.560 So I'm going to use just the name sign up there and then I'll have my trailing karma. 00:47.850 --> 00:48.120 All right. 00:48.270 --> 00:50.580 So going to go ahead and save by you are else. 00:50.580 --> 00:55.590 I got to copy the names, sign up here because this is what I'm going to be using inside of my views. 00:55.890 --> 00:59.190 So let's go ahead and I'm going to do this up near the top here. 00:59.190 --> 01:06.600 We can move down all our to do stuff and we've got to first make this function so I can do my def sign 01:06.600 --> 01:06.870 up. 01:07.830 --> 01:11.280 Which this is going to give us a request object. 01:11.890 --> 01:18.690 OK, something that's really important with this is that we are going to be making it so that if someone 01:18.690 --> 01:22.190 wants to sign up, that they can do it without it. 01:22.240 --> 01:24.710 This we're looking for a post request from the user. 01:24.720 --> 01:30.720 But we have to confirm that when they do this, you know that there's not going to be any issues with 01:30.720 --> 01:33.480 the C.S. R.F., which is the cross site. 01:34.170 --> 01:35.680 I think scripting. 01:36.000 --> 01:42.810 I forget the acronym, but basically it usually protects Django requests from some malicious code that 01:42.810 --> 01:43.890 users can use on sites. 01:43.920 --> 01:46.800 But if it's an API, it's not something we have to worry about. 01:47.250 --> 01:51.450 So in order to use this, we're gonna have to import the code. 01:51.450 --> 01:54.330 So here we're gonna do an at C. 01:54.360 --> 01:57.520 S r f underscore exempt. 01:58.040 --> 01:58.310 OK. 01:58.890 --> 02:02.280 Saying that we're not going to check it on this particular function here. 02:02.580 --> 02:07.980 Now, in order to import this, we're going to have to say from and say with me, because this is a 02:07.980 --> 02:14.290 bit of a big one, we're gonna do Django views, dot decorators. 02:15.150 --> 02:16.740 Dot C. 02:16.830 --> 02:18.240 S r f. 02:19.440 --> 02:20.220 Import. 02:21.220 --> 02:23.140 And then we want to import that C. 02:23.140 --> 02:25.290 S, our F except J. 02:25.930 --> 02:33.040 So now that we've got this in place and I've got to make sure that I always have spell inheres or DEC 02:33.040 --> 02:34.360 or raid tours. 02:34.780 --> 02:35.230 There we go. 02:35.710 --> 02:39.590 So we have this code that says, OK, we'll be able to access this. 02:39.610 --> 02:44.440 The next thing that we need to do is sort of check and make sure that this is what we're looking for. 02:44.500 --> 02:49.230 And the great thing about having an existing project is we can reuse a lot of code. 02:49.270 --> 02:55.390 So if we go to the views that pie of our to do app, we have a ton of code for if someone wants to sign 02:55.390 --> 02:56.890 up a user account. 02:57.040 --> 03:00.530 So I'm going to copy this huge chunk that we have here. 03:00.640 --> 03:00.960 Okay. 03:01.980 --> 03:03.040 Copy all of that. 03:03.060 --> 03:06.240 I'm going to come back to my views dot pie, and I'm going to paste this in. 03:07.140 --> 03:09.270 But we do need to change quite a bit here. 03:09.270 --> 03:11.370 So the first one is we're looking for a post. 03:11.400 --> 03:16.020 We only should accept post because if someone signing up, they're sending information, this should 03:16.020 --> 03:17.280 be a post request. 03:17.670 --> 03:23.310 So I'm gonna go ahead and get rid of the LDS because we're only interested to know if this is a post. 03:23.730 --> 03:30.220 We don't have to check the passwords so we can go ahead and just delete this if statement and the whole 03:30.240 --> 03:34.200 ELT's that comes at the bottom there, we can go ahead and just get rid of that. 03:34.980 --> 03:38.600 And then let's go ahead and selected these lines. 03:38.610 --> 03:41.760 I'm going to tab them in one space, at least in Adam. 03:41.760 --> 03:45.870 If you do shift tab, you can go inside, whereas tab, you know, puts it to the right. 03:45.900 --> 03:47.280 The other way gets you to the left. 03:47.940 --> 03:51.630 Now with this, what we've got to do is we have to create a user object. 03:51.660 --> 03:56.340 Now getting the proper information is not the exact same as before. 03:56.940 --> 03:59.070 We want to use a Jason parser. 03:59.460 --> 04:04.650 So what we're gonna do is import some code here that's going to get us this Jason parser. 04:04.650 --> 04:12.960 So we're going to see from we want rest underscore framework, DOT parsers. 04:15.170 --> 04:18.470 Import and then this is where we want all caps. 04:18.500 --> 04:21.050 Jason pass, sir. 04:22.030 --> 04:22.490 There we go. 04:22.840 --> 04:27.440 Okay, so what we're gonna do is we're gonna get the information that is come back to us. 04:27.440 --> 04:32.000 So we're going to say data is equal to this capital J. 04:32.000 --> 04:34.060 Some passer. 04:35.030 --> 04:37.550 Open and close parentheses dot pass. 04:37.970 --> 04:41.590 And we want to to pass the request that has been sent in. 04:41.670 --> 04:47.210 Okay, so now that we have this data, the data is essentially a dictionary to which we can get to anything 04:47.210 --> 04:47.750 that we want. 04:48.170 --> 04:51.880 So we're just going to take this data in rather than the request up post. 04:51.890 --> 04:57.530 We'll just pass this in here as data and for the password, we'll have that be equal to the data and 04:57.530 --> 04:59.400 go grab whatever the password is. 04:59.420 --> 05:04.700 Now, because when someone's using this API, if someone's gonna be, you know, signing up for a user 05:04.710 --> 05:08.150 account, we don't have to have password one and password to. 05:08.540 --> 05:13.400 Like, if someone, for example, is making a mobile app of this to do project, they probably should 05:13.730 --> 05:17.030 have the user enter in their password twice and confirm that they match. 05:17.360 --> 05:19.280 But they can do that locally on the device. 05:19.280 --> 05:24.450 And then when they go to create an account to sign up, they can just pass us the one password. 05:24.470 --> 05:25.880 It's kind of silly to do that. 05:26.150 --> 05:27.290 Checking here for us. 05:27.350 --> 05:30.230 So I'm going to go ahead and just ask for simply a password. 05:30.260 --> 05:31.580 We don't need password one. 05:32.120 --> 05:36.500 But with that in place, we use all of the same code to be able to create a user. 05:36.680 --> 05:38.480 And we still say user dot save. 05:38.900 --> 05:39.740 That's all fine. 05:40.340 --> 05:43.070 But rather than returning a redirect. 05:43.280 --> 05:48.050 In this case, we want to send back a response and we want to send it via Jason. 05:48.290 --> 05:48.520 Okay. 05:48.860 --> 05:53.180 So you'll notice when we're doing the function based view, we don't get any of the magic that comes 05:53.690 --> 05:56.270 with the class space views in the Django rest framework. 05:56.300 --> 06:00.560 So it's a little bit unfortunate, although like this Jason parser, for example, this comes from the 06:00.560 --> 06:01.640 jingle risk framework. 06:01.640 --> 06:07.010 So there's some cool stuff there, but we kind of have to do a little bit more work when we're making 06:07.010 --> 06:07.850 things custom. 06:08.000 --> 06:12.140 So we're going to import some code to use a Jason response. 06:12.260 --> 06:13.370 So I'm going to say from. 06:14.430 --> 06:15.240 Django. 06:16.750 --> 06:20.170 Spelled that correctly, Django Dot H. 06:20.560 --> 06:22.540 TDP import. 06:23.790 --> 06:30.800 Jay, some response and notice how this, Jason, is not all caps, but the one from the rest framework 06:30.800 --> 06:34.070 is you got to be careful with, you know, which one you're looking at here. 06:34.250 --> 06:40.670 But with this Jason response, we're gonna copy this and say so that when we do a return here, we're 06:40.670 --> 06:42.410 gonna return the Jason response. 06:42.920 --> 06:48.410 We're gonna inside of the parentheses here first pass forward a little dictionary with some information. 06:48.980 --> 06:52.520 We currently don't have it set up, but we're gonna be passing forward a token. 06:52.610 --> 06:58.400 So right now, we'll just say sending forward a token and we'll just put it kind of put in some dummy 06:58.400 --> 06:59.120 data for now. 07:00.490 --> 07:05.770 Besides this dictionary, this Jason, that we're passing back, we're also going to say that the status 07:05.860 --> 07:11.260 of this is equal to two a one two a one means that something was created successfully. 07:12.100 --> 07:19.720 Now, if this user name has already been selected by someone inside of this, except here for the integrity 07:19.720 --> 07:23.410 here, this is where we'd want to return back a Jason response. 07:23.710 --> 07:27.660 But in this case, we would say, you know, something like that user name has already been taken. 07:27.670 --> 07:28.570 Please choose a new one. 07:28.570 --> 07:32.050 So I'm going to copy or delete what we've got here. 07:33.180 --> 07:35.790 So we're gonna return this Jason response. 07:36.210 --> 07:41.160 But we're going to use this little, you know, text that we already had. 07:41.520 --> 07:44.430 Again, I love using the existing project here. 07:44.440 --> 07:45.630 It makes our life so easy. 07:46.090 --> 07:49.230 Gay soldiers return air that user names are already been taking. 07:49.350 --> 07:50.950 Please choose a new username. 07:51.540 --> 07:54.180 And we are going to. 07:55.120 --> 07:56.310 Well, let's make sure. 07:56.340 --> 07:58.910 OK, so we've got the air to the. 08:00.320 --> 08:01.220 That all looks good. 08:01.250 --> 08:05.490 And then the status on this is going to be a four hundred. 08:07.100 --> 08:13.550 So let's go ahead and save this and now test out what we've got to see if we can get this to work. 08:13.590 --> 08:18.650 Now, we don't have any of the quote unquote, actual token stuff working, but let's see if it works. 08:18.660 --> 08:23.720 So we're gonna have to create a pretty complex Kearl situation here. 08:23.720 --> 08:28.400 So I recommend whenever you're making one of these, it's always best to just make a file where you 08:28.400 --> 08:30.740 can sort of list out what you're doing. 08:31.220 --> 08:34.970 So I'm gonna go ahead and do a new file here, command and to do that. 08:34.970 --> 08:36.230 And I'm going to save this. 08:36.650 --> 08:38.540 I'm going to save it onto my desktop here. 08:38.750 --> 08:41.330 So I'm just gonna call this Kearl fun. 08:43.050 --> 08:47.970 So I'm going to start building out a curl and we can use a lot of what we had before, so I'm going 08:47.970 --> 08:51.720 to do the up key here to copy what we've got. 08:52.630 --> 08:53.880 I'm going to copy this. 08:54.360 --> 08:58.140 I mean, really all that we're gonna be using from this, we're not going to use the Deshu, but I like 08:58.140 --> 09:00.300 to just get the local host, all that stuff. 09:00.300 --> 09:02.370 So we're going to do API slash. 09:02.460 --> 09:03.120 Sign up. 09:03.410 --> 09:03.650 OK. 09:04.260 --> 09:10.530 Now with this, what we're looking to do here is basically say, OK, we want to sign up. 09:10.560 --> 09:12.660 We've got to make sure that is a post. 09:12.660 --> 09:13.230 So let's go ahead. 09:13.250 --> 09:16.620 And up at the front here to this dash capital X. 09:17.190 --> 09:21.190 And then in strings, specify that we want to do a post K. 09:21.300 --> 09:22.050 That's the U. 09:22.070 --> 09:24.330 RL that we're looking for there. 09:24.750 --> 09:30.360 The next thing that we want to do is specify that we're gonna be sending Jason's we're going to do a 09:30.360 --> 09:31.890 dash capital H. 09:32.370 --> 09:35.520 Then inside of a string, we're gonna say capital content. 09:36.710 --> 09:37.840 Dash type. 09:37.880 --> 09:42.920 And it's really important that you get the exact spelling and capitalization, all of this, because 09:42.920 --> 09:50.060 this is a standardized way that you specify that you're going to be sending Jason Veha and HTP requests. 09:50.060 --> 09:52.100 So we have content type. 09:52.700 --> 09:54.650 And then space app. 09:54.980 --> 09:58.100 Location Flash Jason. 09:58.920 --> 10:00.350 OK, we've got that all there. 10:01.130 --> 10:06.620 And then at this point we just need to pass forward whatever data's going to be inside of here. 10:07.310 --> 10:11.840 So for us, we have to pass forward a username and a password. 10:12.200 --> 10:17.810 So we're gonna do a dash D for the data or the body that we're passing forward here. 10:18.260 --> 10:22.580 And so inside of a string, we're gonna do our curly brackets. 10:23.060 --> 10:27.080 And inside of these I'm going to say user name. 10:28.450 --> 10:36.250 And let's do a call in here and I'll do Nick CAAMA and then let's also put in a password. 10:36.890 --> 10:37.280 OK? 10:38.050 --> 10:41.560 And we'll do our classic ABC DS one, two, three, four. 10:41.740 --> 10:44.530 So you'll notice this curl that we've created here. 10:45.160 --> 10:48.250 This should be trying to sign up for a user that already exist. 10:48.260 --> 10:52.790 So we should get back the responses air that usernames already been taken. 10:52.810 --> 10:56.080 So let's just go ahead and copy this whole line here. 10:56.890 --> 11:01.660 Come to our Terminal K and let's paste this in. 11:01.710 --> 11:02.410 Hit enter. 11:02.940 --> 11:06.180 And oh, he hit a big, nasty air. 11:06.220 --> 11:07.750 So a pro tip here. 11:08.110 --> 11:13.120 I've messed up somewhere, but if you try and read the air here from your terminal like this, you're 11:13.120 --> 11:14.530 really going to have a bad time. 11:14.620 --> 11:21.070 What I recommend is going to the tab of your terminal or window of your terminal that has the air. 11:21.070 --> 11:23.650 And look at it here, because this is much more readable. 11:23.650 --> 11:26.530 In fact, just at the bottom here, name integrity. 11:26.530 --> 11:27.670 Air is not defined. 11:28.060 --> 11:31.600 This is some code that I forgot to import into the project. 11:31.660 --> 11:38.620 So let's go back to, in fact, actually the views of the do app and let's copy the code that gets that 11:38.620 --> 11:39.180 integrity. 11:39.180 --> 11:41.360 Or is the jingo dot dbe. 11:41.380 --> 11:42.340 Let's copy this. 11:42.970 --> 11:44.530 Come back to the other views. 11:45.160 --> 11:46.120 Paste this in. 11:46.240 --> 11:47.170 Save this. 11:47.320 --> 11:47.620 OK. 11:48.290 --> 11:51.360 That we'll come back to the tab where we're running our Kerl. 11:51.850 --> 11:54.160 Let's hit enter on this and a can. 11:54.280 --> 11:54.910 Another error. 11:54.910 --> 11:55.810 This is not good. 11:56.290 --> 11:59.080 Kate named capital user is not defined. 11:59.200 --> 11:59.570 OK. 11:59.620 --> 12:01.210 So we've got to import that as well. 12:01.560 --> 12:02.590 So let's go back to our views. 12:02.620 --> 12:05.080 This is the problem when you copy and paste a ton of code. 12:05.110 --> 12:07.020 You forget all these import. 12:07.030 --> 12:08.590 So let's go ahead and copy this. 12:09.150 --> 12:11.850 Come back into our project piece that there. 12:12.610 --> 12:15.490 Let's try this yet again one more time. 12:16.870 --> 12:17.680 And look at that. 12:17.890 --> 12:22.150 Finally, after a couple of fixes error, that user name has already been taken. 12:22.180 --> 12:26.290 But let's go ahead and modify this just a little bit here. 12:27.010 --> 12:30.940 So I'm going to change this to be Nick one. 12:31.380 --> 12:31.580 OK. 12:31.780 --> 12:37.390 So I'm going to copy this comeback, paste this into the terminal and. 12:39.580 --> 12:40.660 Still another here. 12:40.720 --> 12:42.640 Name Log-in is not defined. 12:42.760 --> 12:43.180 OK. 12:43.240 --> 12:47.290 You are killing me here with the different code that we have. 12:47.320 --> 12:50.090 So in fact, this is not a line that we need. 12:50.110 --> 12:53.110 We don't need to log in the user so we can delete this. 12:53.620 --> 12:55.510 So let's go ahead and save this. 12:56.130 --> 12:58.510 Come back to our terminal here now. 12:58.530 --> 13:00.870 I imagine that user was actually already created. 13:00.880 --> 13:03.850 So if I do the up arrow and try and create Nick one again. 13:04.480 --> 13:04.840 Yes. 13:04.870 --> 13:06.040 It says that's already been taken. 13:06.040 --> 13:09.250 But let's go ahead and change this to now be Nick two. 13:09.990 --> 13:10.720 And look at that. 13:10.900 --> 13:13.590 It passes us back, our fake tokens. 13:13.600 --> 13:16.480 So it's quote unquote, working. 13:16.570 --> 13:20.650 I know that was a little bit of work there, but hopefully that gives you some better idea how to debug 13:20.650 --> 13:22.270 things when you run into issues. 13:23.020 --> 13:24.580 But this is a big step for us. 13:24.670 --> 13:27.910 Now users can sign up for accounts. 13:27.970 --> 13:28.990 This is a really big deal. 13:28.990 --> 13:34.930 We could, in fact, stop right here and just say, hey, this is how you sign up and we'll use that 13:34.930 --> 13:39.880 basic authentication where we, you know, pass in the dash you to have someone put in the username 13:39.970 --> 13:40.480 and password. 13:40.510 --> 13:44.560 But I really think token authentication is a much better way to do things. 13:44.850 --> 13:47.500 And we'll be talking about that in the next lecture.